<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);
}
}
我在代码上方运行此代码,更改了设置中的时区,而不调用其他活动。谁能说出问题所在?
最佳答案
我遇到了同样的问题,该线程帮助我使TimeZone更新正常工作,但是我仍然没有收到有关日期/时间更改的通知。我终于发现,清单文件中指定的内容与广播接收方在过滤意图时使用的内容有所不同。虽然它已在Android Intent参考中进行了记录,但很容易忽略!
在您的AndroidManifest.xml文件中,使用以下命令:
<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>
在您的接收器类中:
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"); }
}
}
}