正在进行的通知消失在清除​​内存 [英] Ongoing Notification Disappears When Clearing Memory

查看:187
本文介绍了正在进行的通知消失在清除​​内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我想取消正在进行的通知,只有取消
   选项​​在选择应用程序,但每当我清除任务内存
   我的Andr​​oid设备管理器/ RAM选项卡中我的通知消失。

My problem is I want to cancel an ongoing notification only if cancel option is selected in the app, but whenever I clear memory on "Task Manager/RAM" tab of my android device my notification disappears.

我能做些什么使我持续逗留的通知,即使我清晰的记忆?

What can I do to make my ongoing notification stay even if I clear memory?

我创建了一个扩展 BroadcastReciever class.In我OnGoingNotificationReciever(扩展BroadcastReciever)一类正在进行的通知:

I created an ongoing notification in a class that extends BroadcastReciever class.In my OnGoingNotificationReciever(extends BroadcastReciever):

Intent mainAct =  new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 555, mainAct, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(resID).setContentTitle(city).setContentText(temperature);
mBuilder.setContentIntent(contentIntent);
mBuilder.setOngoing(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1,  mBuilder.build());

然后,我设置了 AlarmManager 来启动该BroadcastReciever并每5分钟重复一次:

Then I set an AlarmManager to start this BroadcastReciever and repeat every 5 minutes:

AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
Intent i7 = new Intent(getApplicationContext(), OnGoingNotificationReciever.class);
PendingIntent ServiceManagementIntent = PendingIntent.getBroadcast(getApplicationContext(), 1111111, i7, 0);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime(), 5 * 60 * 1000, ServiceManagementIntent);

这完美的作品和做什么,我想做的事情(例如当我关闭应用程序,通知停留在状态栏上),除了清除内存时消失。

This works perfectly and does what I want to do(e.g when I close the app, notification stays on the status bar) except it disappears when clearing memory.

注: 雅虎天气 应用它做什么,我想做的事情,但我怎么也找不到在互联网上。

Note: In Yahoo Weather application it does what I want to do, but I couldn't find how on the Internet.

推荐答案

很多搜索后,我终于找到了解决这个问题。

After a lot of search I finally found a solution to this problem.

在我的Manifest.xml我写了以下内容:

In my Manifest.xml I wrote the following:

<receiver android:name=".OnGoingNotificationReciever" >
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_RESTARTED" />
        <data android:scheme="package" android:path="com.example.havadurumumenu" />
    </intent-filter>
</receiver>

PACKAGE_RESTARTED 被称为当你清晰的记忆,并设置该内存清空后重新启动您的接收器。

PACKAGE_RESTARTED is called when you clear memory, and setting this restarts your receiver after clearing memory.

这篇关于正在进行的通知消失在清除​​内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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