我在android中的通知有问题我不知道我做错了什么...此通知是在我的服务类(AfterBootService)中创建的,并且在我的接收器类(MyReceiver)代码中完成启动时启动该服务两个类:
MyReceiver.class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, AfterBootService.class);
context.startService(service);
}
}
在这里AfterBootService.class
public class AfterBootService extends Service {
private NotificationManager mNotificationManager;
private static final int NOTIFICATION_ID = 1;
@Override
public void onCreate() {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
showNotification();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onDestroy() {
mNotificationManager.cancel(NOTIFICATION_ID);
Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
/**
* Show a notification while this service is running.
*/
private void showNotification() {
int icon = R.drawable.icon_notification_ribbon;
String tickerText = "Your Notification";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
String expandedText = "Sample Text";
String expandedTitle = "Program Name Here";
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this, IconActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
//notification.flags |= Notification.FLAG_NO_CLEAR;
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(context, expandedTitle, expandedText, contentIntent);
// Send the notification.
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
}
现在,当我启动一切正常时,接收器启动服务和服务启动通知,但是当它显示在状态栏中并在通知窗口中显示适当的消息后,它会在一些(2-3)秒后消失......我没有设置任何东西允许该消息消失(我想我不是专家)。我想知道的:
- 当我的服务正在运行时,如何始终保留该通知消息?
- 是否可以在不运行启动该通知的服务的情况下保留所有时间通知?为了更仔细地解释它,可以在使用 stopSelf() 销毁服务之后在服务中启动通知并仍然保留该通知消息?
如果需要,我可以在这里粘贴我的清单。谢谢