<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);
}
}
我运行上面的代码更改了不调用其他活动的设置中的时区。任何人都能说出问题所在吗?
答案 0 :(得分:3)
我遇到了同样的问题,这个帖子帮助我让TimeZone更新工作,但是我仍然没有收到日期/时间更改的通知。我终于发现你在清单文件中指定的内容与广播接收器在过滤意图时使用的内容有所不同。虽然在Android Intent参考文献中记录了 IS ,但它很容易被忽视!
在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"); }
}
}
}
答案 1 :(得分:1)
它没有用,因为意图是错误的。将以上内容替换为:
<intent-filter>
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.TIME" />
</intent-filter>
然后转到“设置”区域并更改时区。
答案 2 :(得分:0)
我猜你的AndroidManifext.xml
中没有包含以下几行,是吗?
<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>