我有:
MyApp 使用 onCreate 扩展应用程序:
sendBroadcast(refreshAlarm);
Log.d(TAG, "broadcast sent with intent " + refreshAlarm);
Log.d(TAG, "onCreate");
在哪里
static final Intent refreshAlarm = new Intent(ACTION_REFRESH_RECEIVER);
public static final String ACTION_REFRESH_RECEIVER = "com.example.myapp.REFRESH_RECEIVER";
广播接收器:
package com.example.myapp;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.util.Log;
public class RefreshReceiver extends BroadcastReceiver
{
private static final String TAG = "RefreshReceiver";
@Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, "broadcast received with intent " + intent);
long interval = Long
.parseLong(PreferenceManager.getDefaultSharedPreferences(
context).getString("delay", "900")) * 1000;
PendingIntent operation = PendingIntent.getService(context, -1,
new Intent(context, RefreshService.class),
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(operation);
if (interval > 0)
{
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), interval, operation);
}
Log.d(TAG, "onReceive: delay = " + interval);
}
}
在清单中声明:
<receiver android:name="com.example.myapp.RefreshReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="com.example.myapp.REFRESH_RECEIVER" />
</intent-filter>
</receiver>
在我看来,我拥有完成这项工作所需的一切。广播是在 onCreate 中发送的(我可以在日志中看到它确实是发送的)。使用意图过滤器声明广播以接收refreshAlarm
意图,但它没有收到它,我不知道为什么。我还需要什么吗?