从最近的应用程序中删除应用程序后,AlarmManager停止 [英] AlarmManager Stops after removing app from recents apps

查看:108
本文介绍了从最近的应用程序中删除应用程序后,AlarmManager停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新手,在这里,我的目标是使用警报管理器每2分钟运行一个代码段,该代码段将轮询服务器(使用网站的api),并基于返回的JSON生成通知。
上网查询后,我认为最好的选择之一就是使用Intent服务和android。

I am new to this part of android, and here I aim to use alarm manager to run a code snippet every 2 minute which will poll a server (using the website's api) and based on the returned JSON generate notification. After a looking up the web I thought one of the best option in my case will be using intent service and android.

服务和接收清单

<service
    android:name=".NotifyService"
    android:enabled="true"
    android:exported="false" >
</service>
<receiver
    android:name=".TheReceiver"
    android:enabled="true"
    android:exported="true" >
</receiver>
<receiver
    android:name=".OnOffReceiver"
    android:enabled="true"
    android:exported="true" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

参与Flash屏幕活动,在其中我称为意图服务,该服务负责轮询通知:

Part in the flash screen activity where I call the intent service which is responsible for polling for notification:

Intent msgIntent = new Intent(this, NotifyService.class);
    startService(msgIntent);

接收方在设备启动时启动警报:

The receiver to start the alarm on device start:

public class OnOffReceiver extends BroadcastReceiver
{
    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;
    public OnOffReceiver(){}
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent service = new Intent(context, NotifyService.class);
        service.setAction(NotifyService.CREATE);
        context.startService(service);
    }
} 

IntentService类

The IntentService Class

public class NotifyService extends IntentService
{
    public NotifyService()
    {
        super("NotifyService");
    }
    public static final int STATUS_RUNNING = 0;
    public static final int STATUS_FINISHED = 1;
    public static final int STATUS_ERROR = 2;

    @Override
    protected void onHandleIntent(Intent intent)
    {
        if (intent != null)
        {
            final String action = intent.getAction();
        }
        StartStuff();
    }

    public void StartStuff()
    {
        Intent intent = new Intent(this, TheReceiver.class);
        PendingIntent pend_intent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,1200,1200, pend_intent); 
     //1200ms to make it easier to test
    }

}

设置通知的接收器类,为了测试目的,我在这里没有做任何与网络相关的工作,只是发出一个简单的通知来检查应用程序是否在所有情况下都运行

The receiver class which sets notification, for testing pupose I am not doing any network related work here just making a simple notification to check if the app is running in all situations

public class TheReceiver extends BroadcastReceiver
{
    public TheReceiver(){}
    @Override
    public void onReceive(Context context, Intent intent)
    {
         Toast.makeText(context, " Success ", Toast.LENGTH_SHORT).show();
         Log.d("Notification", "The Receiver Successful");
        showNotification(context);

    }
    private void showNotification(Context context)
    {         
        NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context).setContentTitle("My notification").setContentText("Hello World!");
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());

    }  
} 

不过,只有在应用正在运行或在最近的应用托盘中。
它不会开始通知手机何时重启,也不会在从最近的应用程序托盘中删除该应用程序后发出通知。

However the notification come only when the app is running or in the recent apps tray. It does not start notifying when the phone reboots, nor does it notify after the app is removes from the recent apps tray.

该应用程序需要像即使其他应用程式(例如gmail,whatsapp)从最近的应用程式匣中滑出,也可以。
及时性和守时性不是什么大问题,因为可以忍受5到10分钟的延迟。 (不过我打算每2分钟进行一次投票。)

The app needs Notify users like other apps (like gmail, whatsapp) do, even if they are swiped out of the recent apps tray. Timeliness and punctuality are not very big issue as delay up to 5 to 10 minutes are tolerable. (I intend to poll ever 2 minutes though.)

我要去哪里错了?另外,还有更好的方法解决此问题吗?

Where am I going wrong? Also, is there a better way to go about the problem?

推荐答案

在关闭应用程序后保持接收器处于活动状态是为了使用

To keep a receiver active after closing the app is to use

android:process=":remote"

清单文件中的

需要保持活动的接收者。

in the manifest file for the receiver that needs to be kept alive.

 <receiver
        android:name=".TheAlarmReceiver"
        android:process=":remote">
 </receiver>

在接收者清单(本例中为TheReceiver)中,我们需要在应用程序之后保持活动状态关闭。

in the manifest for the receiver (TheReceiver in this case) that we need to keep active after the app closes.

PS :我还更改了对应用程序使用IntentsService和AlarmManager的方式,因为以前的实现不是解决它的好方法。

P.S. : I also changed the way I use IntentsService and AlarmManager for the application, as my previous(above) implementation is not a very good way to go around it.

这篇关于从最近的应用程序中删除应用程序后,AlarmManager停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆