2010-08-06 59 views

回答

2

看一看這樣的:

  1. http://developer.android.com/reference/android/content/BroadcastReceiver.html
  2. http://developer.android.com/guide/topics/manifest/receiver-element.html

寫在AndroidManifest.xml文件下面的代碼:

<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> 

並定義如下的類:

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)); 
      } 
     } 
    } 

} 

並擁有WifiDemo例如, click Here

+1

非常感謝Paresh。 – praveenb 2010-08-06 12:16:27