开发者

How i can use broad cast receiver in android

开发者 https://www.devze.com 2023-01-10 23:39 出处:网络
How i can use broad cast receiver in android,开发者_JS百科 Please give me example or a reference to follow.

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:

  1. http://developer.android.com/reference/android/content/BroadcastReceiver.html
  2. 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 .

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号