我刚刚使用警报管理器启动广播接收器和广播接收器呼叫服务。在我已经在警报服务中设置的固定时间间隔之后,一次又一次地调用整个过程。但是我仍然遇到一个问题,我的服务在一些Android设备中一两天后就会被杀死。以下是我的代码。
这是我从中调用广播接收器的地方。
Intent myIntent = new Intent(NotificationFirstActivity.this,
MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(
NotificationFirstActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(),
pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1 * 60 * 1000, pendingIntent);
这是我的广播接收器,如下所示。
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent pushIntent = new Intent(context, MyAlarmService.class);
context.startService(pushIntent);
}
}
}
以下是我的明显部分,如。
<service android:name="MyAlarmService"
android:enabled="true" />
<receiver android:name="MyReceiver"/>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="com.phoenixmanage" />
</intent-filter>
</receiver>
<service android:name="com.phoenixmanage.GCMIntentService" />
答案 0 :(得分:2)
这是我在我的一个应用程序中使用的,完美地工作。我用它来每15分钟在服务器上发布数据。
public class BackgroundService extends Service {
public static final String ACTION_PING = "com.example.ACTION_PING";
public static final String ACTION_CONNECT = "com.example.ACTION_CONNECT";
public static final String ACTION_SHUT_DOWN ="com.example.ACTION_SHUT_DOWN";
private final static String TAG = "BackgroundService";
static Context context;
private static volatile PowerManager.WakeLock lockStatic = null;
long INTERVAL =AlarmManager.INTERVAL_FIFTEEN_MINUTES;
public static Intent startIntent(Context context) {
Intent i = new Intent(context, BackgroundService.class);
i.setAction(ACTION_CONNECT);
return i;
}
public static Intent pingIntent(Context context) {
Intent i = new Intent(context, BackgroundService.class);
i.setAction(ACTION_PING);
return i;
}
public static Intent closeIntent(Context context) {
Intent i = new Intent(context, BackgroundService.class);
i.setAction(ACTION_SHUT_DOWN);
return i;
}
synchronized private static PowerManager.WakeLock getLock(Context context) {
if (lockStatic == null) {
PowerManager mgr = (PowerManager) context.getApplicationContext().getSystemService(Context.POWER_SERVICE);
lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
lockStatic.setReferenceCounted(true);
}
return (lockStatic);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
context = this;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i(TAG, "onStart");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Log.i(TAG, "onStartCommand");
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock lock = getLock(this.getApplicationContext());
if (!lock.isHeld()) {
lock.acquire();
}
if (intent != null) {
if (ACTION_SHUT_DOWN.equals(intent.getAction())) {
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
}
if (intent == null || (intent.getAction() != null && !intent.getAction().equals(ACTION_SHUT_DOWN))) {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent operation = PendingIntent.getService(this, 0,
BackgroundService.pingIntent(this),
PendingIntent.FLAG_NO_CREATE);
if (operation == null) {
PendingIntent operation = PendingIntent.getService(this, 0, BackgroundService.pingIntent(this), PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pm.isScreenOn() ? SCREEN_ON_INTERVAL : SCREEN_OFF_INTERVAL, operation);
}
}
//fetch parsms and make your web service call here
return START_STICKY;
}
使用它来启动服务
startService(BackgroundService.startIntent(context));
使用它来终止服务
AlarmManager am = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
PendingIntent operation = PendingIntent.getService(context, 0,
BackgroundService.pingIntent(context),
PendingIntent.FLAG_NO_CREATE);
if (operation != null) {
am.cancel(operation);
operation.cancel();
}
context.startService(BackgroundService.closeIntent(context));
注意:如果您的设备重启,您的闹钟会被取消,所以如果您想重新创建它,您应该创建一个侦听Android.intent.action.BOOT_COMPLETED动作的BroadcastReceiver并允许您调用上面的代码来设置重复警报。
答案 1 :(得分:0)
可能有以下事情发生。 1)如果设备关闭并启动,那么您将无法获得警报广播接收器。实施 OnBootReceiver ,它将接收 OnBoot已完成,您可以在那里启动未触发的待处理警报。 参考:Android - Alarm doesn`t fire sometimes after long period
2)http://www.learn-android-easily.com/2013/06/scheduling-task-using-alarm-manager.html