开发者

time zone example not working in emulator

<receiver android:name=".ExampleBroadCastReceiver" 
    android:enabled="true"> 
    <intent-filter> 
        <action android:name="android.intent.action.ACTION_TIMEZONE_CHANGED"/> 
    </intent-filter> 
</receiver> 
package com.broadcastreceiver;

import java.util.ArrayList;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class ExampleBroadCastReceiver extends BroadcastReceiver { 
    @Override
    public void onReceive(final Context context, final Intent intent) {
        // TODO Auto-generated method stub

        Log开发者_运维问答.d("ExmampleBroadcastReceiver", "intent=" + intent); 
        Intent intent1 = new Intent(context,Login.class); 
        context.startActivity(intent1); 
    } 
} 

I run this above code change the time zone in settings not calling other activity. Can anybody tell what the problem is?


I was having the same issue, and this thread helped me get TimeZone updates working, however I still wasn't getting notifications for Date/Time changes. I finally found that there's a difference in what you specify in your manifest file and what your broadcast receiver uses when filtering intents. While it IS documented in the Android Intent reference, it is very easy to overlook!

In your AndroidManifest.xml file, use the following:

  <receiver android:name=".MyReceiver">
    <intent-filter>
      <!-- NOTE: action.TIME_SET maps to an Intent.TIME_CHANGED broadcast message -->
      <action android:name="android.intent.action.TIME_SET" /> 
      <action android:name="android.intent.action.TIMEZONE_CHANGED" />
      <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
  </receiver>

And in your receiver class:

public class MyReceiver extends BroadcastReceiver {
  private static final String TAG = "MyReceiver";
  private static boolean DEBUG = true;

  @Override
  public void onReceive(Context context, Intent intent) {
    final String PROC = "onReceive";

    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        if (DEBUG) { Log.v(TAG, PROC + ": ACTION_BOOT_COMPLETED received"); }
    }
    // NOTE: this was triggered by action.TIME_SET in the manifest file!
    else if (intent.getAction().equals(Intent.ACTION_TIME_CHANGED)) {
      if (DEBUG) { Log.v(TAG, PROC + ": ACTION_TIME_CHANGED received"); }
    }
    else if (intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
      if (DEBUG) { Log.v(TAG, PROC + ": ACTION_TIMEZONE_CHANGED received"); }
    }
  }

}


It wasn't working because the intents are wrong. Replace the ones above with:

<intent-filter>
    <action android:name="android.intent.action.TIMEZONE_CHANGED" />
    <action android:name="android.intent.action.TIME" />
</intent-filter>

Then go to the Settings area and change the timezone.


I guess you didn't include the following lines in your AndroidManifext.xml, did you?

 <receiver android:name=".ExampleBroadcastReceiver" android:enabled="false">
     <intent-filter>
         <action android:name="android.intent.ACTION_TIMEZONE_CHANGED" />
         <action android:name="android.intent.ACTION_TIME" />
     </intent-filter>
 </receiver>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜