报警管理器和广播接收器有效关闭app

时间:2017-11-20 08:38:54

标签: android android-broadcast android-alarms android-broadcastreceiver

我想定期做一些网络工作,即使在强制关闭的应用程序中也是如此。

现在它一直有效,直到它被关闭。 我错过了什么?

此外,如果我添加清单显示:android:process =“:remote” - 它不触发onReceive方法(就像app强制关闭),但在日志中我发现:

V / AlarmManager:已触发:cmp = com.cryptorulezz.cryptosignals / .Code.Alarm Pkg:com.cryptorulezz.cryptosignals

我的代码:

public class Alarm extends BroadcastReceiver
{
     @Override
     public void onReceive(Context context, Intent intent)
     {
         PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
         PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
         wl.acquire();

        // Put here YOUR code.
        Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
        System.out.println("ALARM !!!!!!!!!!!!!!!!!!!!");

        wl.release();
    }

public void setAlarm(Context context)
{
    AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    //Intent i = new Intent(context, Alarm.class);
    Intent i = new Intent("Coinsider.START_ALARM");
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 1, pi); // Millisec * Second * Minute
}

    public void cancelAlarm(Context context)
    {
        Intent intent = new Intent(context, Alarm.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    }
}

我如何在MainActivity中设置闹钟:

Alarm alarm = new Alarm();
alarm.setAlarm(this);

清单:

 <receiver android:name=".Code.Alarm" android:exported="false">
        <intent-filter>
            <action android:name="Coinsider.START_ALARM" >
            </action>
        </intent-filter>
    </receiver>

1 个答案:

答案 0 :(得分:0)

一旦应用程序被强制杀死,它就不会收到意图,并且意图过滤器不会被触发。为了克服这个问题,我建议使用一种依赖于某些系统事件(如android.intent.action.BOOT_COMPLETED)的watchDog,它只是检查你的某个进程是否处于活动状态,如果没有则触发它。在清单中,你有类似的东西:

    <receiver
        android:name="it.angelic.receivers.WatchDogSetupReceiver"
        android:process=":souliss_process">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.USER_PRESENT"/>
        </intent-filter>
    </receiver>

然后班级WatchDogSetupReceiver将检查狗是否还活着,如果需要,可以开一个新的狗:

        Intent i = new Intent(ctx, WatchDogEventReceiver.class); // explicit intent
        PendingIntent patTheDog = PendingIntent.getBroadcast(ctx, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), 5000,
                patTheDog);

最后,WatchDogEventReceiver只会执行所需的不可杀取的工作。看门狗保持亮度很重要,因为它会在事件的每个屏幕上被触发。这个解决方案不是最优的,但即使在强制终止之后也能正常工作:

public class WatchDogEventReceiver extends BroadcastReceiver {


@Override
public void onReceive(final Context ctx, final Intent intent) {
    Log.d(Constants.TAG + ":WatchDog", "WatchDog.onReceive() called, looking for Dataservice:");
    ActivityManager manager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
    //Fire Service  
    Intent eventService = new Intent(ctx, YourDamnService.class);
    ctx.startService(eventService);//sempre, ci pensa poi lui


}