在Android中广播有两种实现方式:
两种广播有如下区别:
通过一个简单的例子说明静态和动态的用法:
(这里用到了widget作为桌面响应的显示)
widget_provider.xml:
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="80dp"
android:minWidth="200dp"
android:initialKeyguardLayout="@layout/widget_layout">
appwidget-provider>
widget_layout.xml:
<LinearLayout
android:id="@+id/widget"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<TextView
android:id="@+id/widget_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
android:textColor="#000000"
/>
LinearLayout>
widget事件处理类-MyWidgetProvider.java:
package com.nightonke.ex_05;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class MyWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteViews.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
}
当然不要忘记在AndroidManifest.xml中对widget进行声明。
<receiver
android:name=".MyWidgetProvider"
android:label="@string/app_name">
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_provider"
>
meta-data>
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
intent-filter>
receiver>
首先,对于静态广播,我们需要在AndroidManifest.xml声明。
<receiver android:name=".MyStaticBroadcastReceiver"/>
然后实现静态广播的接收器,在其中对widget进行更新。
public class MyStaticBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteViews.setTextViewText(R.id.widget_text, intent.getStringExtra("MESSAGE"));
AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(
context.getApplicationContext(), MyWidgetProvider.class), remoteViews);
}
}
静态广播的发送也很简单:
sendStaticBroadcastButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MyStaticBroadcastReceiver.class);
intent.putExtra("MESSAGE", editText.getText().toString());
sendBroadcast(intent);
}
});
可以看到,静态广播不需要在代码中注册。
首先不需要在AndroidManifest.xml声明。
同样的,我们需要一个接收器,在里面对widget进行更新。
public class MyDynamicBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteViews.setTextViewText(R.id.widget_text, intent.getStringExtra("MESSAGE"));
AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(
context.getApplicationContext(), MyWidgetProvider.class), remoteViews);
}
}
最后,动态广播的发送,需要注册才会被接收器接收到。
myDynamicBroadcastReceiver = new MyDynamicBroadcastReceiver();
sendDynamicBroadcastButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// send a dynamic broadcast
// it can be received when registered
Intent intent = new Intent("android.appwidget.action.APPWIDGET_UPDATE");
intent.putExtra("MESSAGE", editText.getText().toString());
sendBroadcast(intent);
}
});
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentFilter dynamic_filter = new IntentFilter();
dynamic_filter.addAction("android.appwidget.action.APPWIDGET_UPDATE");
if (registered) {
unregisterReceiver(myDynamicBroadcastReceiver);
registerButton.setText("Register");
} else {
registerReceiver(myDynamicBroadcastReceiver, dynamic_filter);
registerButton.setText("Unregister");
}
registered = !registered;
}
});
结果:
代码在此