How i can use broad cast receiver in android,开发者_JS百科
Please give me example or a reference to follow.
Thanks
Have a look at this:
- http://developer.android.com/reference/android/content/BroadcastReceiver.html
- http://developer.android.com/guide/topics/manifest/receiver-element.html
Write the below code in AndroidManifest.xml file:
<receiver android:name=".appwidget.ExampleBroadcastReceiver" android:enabled="false">
<intent-filter>
<action android:name="android.intent.ACTION_TIMEZONE_CHANGED" />
<action android:name="android.intent.ACTION_TIME" />
</intent-filter>
</receiver>
And define the class as below:
public class ExampleBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("ExmampleBroadcastReceiver", "intent=" + intent);
// For our example, we'll also update all of the widgets when the timezone
// changes, or the user or network sets the time.
String action = intent.getAction();
if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)
|| action.equals(Intent.ACTION_TIME_CHANGED)) {
AppWidgetManager gm = AppWidgetManager.getInstance(context);
ArrayList<Integer> appWidgetIds = new ArrayList<Integer>();
ArrayList<String> texts = new ArrayList<String>();
ExampleAppWidgetConfigure.loadAllTitlePrefs(context, appWidgetIds, texts);
final int N = appWidgetIds.size();
for (int i=0; i<N; i++) {
ExampleAppWidgetProvider.updateAppWidget(context,
gm, appWidgetIds.get(i), texts.get(i));
}
}
}
}
And have a WifiDemo example, click Here .
精彩评论