我在android中的通知有问题我不知道我做错了什么... 此通知在我的Service类(AfterBootService)中创建,并且在以下两个类的Receiver类(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)秒后显示正确消息IT DISAPPEAR ...我没有'设置任何允许该消息消失的东西(我想我不是专家)。我想知道的是:
如果需要,我可以在此处粘贴我的清单。 THX
答案 0 :(得分:2)
编辑你的破坏方法
从:
public void onDestroy() {
mNotificationManager.cancel(NOTIFICATION_ID);
Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
}
到
public void onDestroy() {
super.onDestroy();
}
因为它取消了你的通知,它会一直保留到用户清除它为止;)
如果没有来自reciver的服务,你可以触发notiffication,并且可以添加
notification.flags = Notification.FLAG_ONGOING_EVENT;
将其设置为不可清除