即使应用程序被杀,也要让alarmmanager保持活动状态

时间:2016-10-15 10:18:34

标签: android service alarmmanager android-broadcastreceiver

我有以下代码,我从MainActivity开始,如果应用程序没有被杀死或关闭,它的工作正常。但是如果应用程序被杀/关闭并且手机正在睡觉它就无法工作。

App Killed / closed + phone sleep =闹钟停止

App Killed / closed + screen on =闹钟正常

App live + phone sleep =闹钟正常

即使应用被杀,手机正在睡觉,我也需要报警。

AlarmLocation.class:

  public void onReceive(Context context, Intent intent) {

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My WakeLock");
    wl.acquire();

    Log.i("ALARM","ID: "+id);
    setAlarm(context);

    //job ends
    wl.release();
}


public void setAlarm(Context context){
    Log.i("SERVICE","ALARM");
    AlarmManager am =( AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, AlarmLocation.class);
    id++;
    PendingIntent pi = PendingIntent.getBroadcast(context, id, i, PendingIntent.FLAG_UPDATE_CURRENT);
    cancelAlarm(context);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+1000*60*1,pi);
            Log.i("ALARM","M");
        }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    am.setExact(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+1000*60*1,pi);
            Log.i("ALARM","KITKAT");
        } else {

            am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),1000*60*1,pi);

        }


}

public void cancelAlarm(Context context){
    Intent intent = new Intent(context, AlarmLocation.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, id-1, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pi);
}

服务:

   private AlarmLocation al= new AlarmLocation();

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("SERVICE","OSM");
    NotificationCompat.Builder notify = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.cast_ic_notification_small_icon)
            .setContentTitle("TITLE")
            .setContentText("TEXT");
    startForeground(1,notify.build());

    al.setAlarm(this);
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}

@Override
public void onStart(Intent intent, int startId) {
    Log.i("SERVICE","OS");
    //al.setAlarm(this);
    super.onStart(intent, startId);
}

@Override
public void onCreate() {

    super.onCreate();
}

@Override
public void onDestroy() {

    Log.i("SERVICE","DESTROYED");
    super.onDestroy();
}

在MainActivity中:

startService(new Intent(this,LocationChecker.class));

1 个答案:

答案 0 :(得分:0)

这有点乱。

这是您需要的服务:

public class NotificationService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        synchronized (this) {
            NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(title)
                    .setContentText(text);
            if (isSound)
                mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
            NotificationManager mNotificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(1, mBuilder.build());
        }
        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        //TODO for communication return IBinder implementation
        return null;
    }
}

在MainActivity的OnCreate功能中,您应该启动AlarmManager:

Intent intent = new Intent(MainActivity.this, NotificationService.class);
PendingIntent pintent = PendingIntent.getService(MainActivity.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, (cal.getTimeInMillis()/interval)*interval+interval, interval, pintent);

不要忘记在AndroidManifest.xml中启用振动:

<uses-permission android:name="android.permission.VIBRATE"/>