在android中清除内存时正在进行的通知消失? [英] Ongoing Notification Disappears When Clearing Memory from the device in android?
问题描述
我的问题是每当我清除我的android设备上的任务管理器/内存选项卡上的内存时,我的通知消失。
我可以做些什么来使即使通知仍然存在,即使我清除记忆?
这是我发送通知的类: -
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
private final static String TAG =GcmIntentService;
private Context mContext;
public GcmIntentService(){
super(GcmIntentService);
@Override
protected void onHandleIntent(意图意图){
Bundle extras = intent.getExtras();
Log.d(TAG,Notification Data Json:+ extras.getString(message));
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
字符串messageType = gcm.getMessageType(intent);
$ b $ if(!extras.isEmpty()){
if(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)){
sendNotification(Send error:+ extras.toString());
} else if(GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)){
sendNotification(服务器上的已删除邮件:+ extras.toString());
} else if(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)){
//此循环表示服务正在进行一些工作。
for(int i = 0; i <5; i ++){
Log.d(TAG,Working ...+(i + 1)+/ 5 @
+ SystemClock.elapsedRealtime());
尝试{
Thread.sleep(5000);
} catch(InterruptedException e){
e.printStackTrace();
$ b Log.i(TAG,Completed work @+ SystemClock.elapsedRealtime());
sendNotification(extras.getString(message));
}
} //释放WakefulBroadcastReceiver提供的唤醒锁。
GcmBroadcastReceiver.completeWakefulIntent(intent);
} //将消息放入通知中并发布。
private void sendNotification(String msg){
mNotificationManager =(NotificationManager)this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(KEY,aman);
PendingIntent contentIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =(NotificationCompat.Builder)new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.sym_def_app_icon)
.setContentTitle (Telepoh)
.setStyle(new NotificationCompat.BigTextStyle()。bigText(msg))
.setContentText(msg)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
mBuilder.setContentIntent(contentIntent);
mBuilder.getNotification()。flags | = Notification.FLAG_AUTO_CANCEL;
mBuilder.setAutoCancel(true);
mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build());
$ b $ private int getNotificationIcon(){
boolean useWhiteIcon =(Build.VERSION.SDK_INT> = Build.VERSION_CODES.LOLLIPOP);
返回useWhiteIcon? R.drawable.gcm:R.drawable.push_icon;
$ b这是我的GcmBroadcastReceiver类: - public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context,Intent intent)
{ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
startWakefulService(context,(intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
$ b $ p $这些是我的清单文件: <?xml version =1.0encoding =utf-8?>
< manifest xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android.com/tools
package =package_name
android:versionCode =2
android:versionName =1.2>
<使用权限android:name =android.permission.ACCESS_FINE_LOCATION/>
< uses-permission android:name =android.permission.INTERNET/>
< uses-permission android:name =android.permission.VIBRATE/>
< permission android:name =android.permission.ACCESS_FINE_LOCATION/>
< permission android:name =android.permission.ACCESS_COARSE_LOCATION/>
<使用权限android:name =android.permission.ACCESS_NETWORK_STATE/>
<使用权限android:name =android.permission.READ_PHONE_STATE/>
<使用权限android:name =android.permission.GET_ACCOUNTS/>
<使用权限android:name =android.permission.WAKE_LOCK/>
< uses-permission android:name =android.permission.USE_CREDENTIALS/>
<使用权限android:name =android.permission.CAMERA/>
< uses-permission android:name =android.permission.WRITE_EXTERNAL_STORAGE/>
< permission android:name =android.permission.WRITE_EXTERNAL_STORAGE/>
<使用权限android:name =android.permission.READ_EXTERNAL_STORAGE/>
< permission android:name =android.permission.READ_EXTERNAL_STORAGE/>
< permission android:name =android.permission.GET_ACCOUNTS/>
< permission android:name =android.permission.CAMERA/>
< permission
android:name =com.google.android.gcm.demo.app.permission.C2D_MESSAGE
android:protectionLevel =signature/>
<使用权限android:name =com.google.android.gcm.demo.app.permission.C2D_MESSAGE/>
<! - 这个应用程序有权注册和接收数据信息。 - >
< uses-permission android:name =com.google.android.c2dm.permission.RECEIVE/>
< application
android:allowBackup =true
android:hardwareAccelerated =true
android:icon =@ mipmap / ic_launcher
android:label =@ string / app_name
android:largeHeap =true
android:screenOrientation =portrait
android:supportsRtl =true
android:theme =@ style / AppTheme
tools:replace =android:icon>
< meta-data
android:name =com.google.android.maps.v2.API_KEY
android:value =API_KEY/>
android:name =。WelcomeActivity
android:label =@ string / app_name
android:theme =@ style / AppTheme .NoActionBar>
< intent-filter>
< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>
< activity
android:name =。MainActivity
android:label =@ string / app_name
android:launchMode =singleTop
android:主题= @风格/ AppTheme.NoActionBar >< /活性GT;
< activity
android:name =。LoginActivity
android:theme =@ style / AppTheme.NoActionBar/>
< activity
android:name =。RegisterActivity
android:theme =@ style / AppTheme.NoActionBar/>
< activity android:name =。ForgetPasswordActivity/>
< activity android:name =pacahe_name.controller.GalleryUtil/>
< activity android:name =nl.changer.polypicker.ImagePickerActivity/>
< receiver
android:name =package_name.GCM.GcmBroadcastReceiver
android:permission =com.google.android.c2dm.permission.SEND>
< intent-filter>
<! - - 接收实际的信息。 - >
$ b < / intent-filter>
< / receiver>
< service android:name =package_name.GCM.GcmIntentService/>
< activity
android:name =com.facebook.FacebookActivity
android:screenOrientation =portrait/>
< meta-data
android:name =com.facebook.sdk.ApplicationId
android:value =@ string / fb_id/>
< meta-data
android:name =com.google.android.gms.version
android:value = @ integer / google_play_services_version/>
< / application>
< / manifest>
div class =h2_lin>解决方案
尝试按照此中的答案Ť通过在您的Manifest文件中使用< action android:name =android.intent.action.PACKAGE_RESTARTED/>
$ b
根据它,清除内存时会调用 PACKAGE_RESTARTED
,并在清除内存后重新启动接收器。
有关更多信息,请查看此文档关于ACTION_PACKAGE_RESTARTED
My problem is 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?
This is the class by which i send the notification :-
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
private final static String TAG = "GcmIntentService";
private Context mContext;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
Log.d(TAG, "Notification Data Json :" + extras.getString("message"));
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification("Deleted messages on server: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
// This loop represents the service doing some work.
for (int i = 0; i < 5; i++) {
Log.d(TAG, " Working... " + (i + 1) + "/5 @ "
+ SystemClock.elapsedRealtime());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
sendNotification(extras.getString("message"));
}
} // Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
} // Put the message into a notification and post it.
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("KEY","aman");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.sym_def_app_icon)
.setContentTitle("Telepoh")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
mBuilder.setContentIntent(contentIntent);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
mBuilder.setAutoCancel(true);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
private int getNotificationIcon() {
boolean useWhiteIcon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.drawable.gcm : R.drawable.push_icon;
}
}
This is my GcmBroadcastReceiver class :-
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{ ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
This is my Manifest File :-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="package_name"
android:versionCode="2"
android:versionName="1.2">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<permission android:name="android.permission.GET_ACCOUNTS" />
<permission android:name="android.permission.CAMERA" />
<permission
android:name="com.google.android.gcm.demo.app.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.google.android.gcm.demo.app.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:icon">
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="API_KEY" />
<activity
android:name=".WelcomeActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@style/AppTheme.NoActionBar"></activity>
<activity
android:name=".LoginActivity"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".RegisterActivity"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".ForgetPasswordActivity" />
<activity android:name="pacahe_name.controller.GalleryUtil" />
<activity android:name="nl.changer.polypicker.ImagePickerActivity" />
<receiver
android:name="package_name.GCM.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name=package_name" />
</intent-filter>
</receiver>
<service android:name="package_name.GCM.GcmIntentService" />
<activity
android:name="com.facebook.FacebookActivity"
android:screenOrientation="portrait" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/fb_id" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
Try to follow the answer in this thread by using the <action android:name="android.intent.action.PACKAGE_RESTARTED" />
in your Manifest file.
According to it, PACKAGE_RESTARTED
is called when you clear memory, and setting this restarts your receiver after clearing memory.
For more information, check this documentation about ACTION_PACKAGE_RESTARTED
这篇关于在android中清除内存时正在进行的通知消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!