【发布时间】:2012-08-25 19:59:03
【问题描述】:
我在 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) 秒后消失...我没有t 设置任何允许该消息消失的东西(我想我不是专家)。我想知道的:
- 如何在我的服务运行时始终保留该通知消息?
- 是否可以在不运行启动该通知的服务的情况下保留所有时间通知?为了更仔细地解释它,可以在使用 stopSelf() 销毁服务之后在服务中启动通知并仍然保留该通知消息?
如果需要,我可以在此处粘贴我的清单。谢谢
【问题讨论】:
标签: android service notifications