I have a service which needs to write a notification on the status bar when some event occurs
我有一项服务,需要在某些事件发生时在状态栏上写一个通知
package com.octoplus.client.services;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import com.octoplus.client.MainActivity;
import com.octoplus.client.R;
import java.util.concurrent.TimeUnit;
public class notif extends Service {
public notif() {
}
NotificationManager nm;
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
public int onStartCommand(Intent intent, int flags, int startId) {
sendNotif();
return super.onStartCommand(intent, flags, startId);
}
void sendNotif() {
// 1-я часть
Notification notif = new Notification(R.drawable.ic_launcher, "We create gif", System.currentTimeMillis());
// ставим флаг, чтобы уведомление пропало после нажатия
notif.flags |= Notification.FLAG_AUTO_CANCEL;
// отправляем
nm.notify(1, notif);
/*
// 3-я часть
Intent intent = new Intent(this, MainActivity.class);
// intent.putExtra(MainActivity.FILE_NAME, "somefile");
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// 2-я часть
notif.setLatestEventInfo(this, "Notification's title", "Notification's text", pIntent);
*/
}
}
I call it with use this code
我用这个代码调用它
startService(new Intent(MainActivity.this, notif.class));
but I made some mistake and I always get this exception
但我犯了一些错误,我总是得到这个例外
java.lang.RuntimeException: Unable to start service com.octoplus.client.services.notif@5334d2c8 with Intent { cmp=com.octoplus.client/.services.notif }: java.lang.IllegalArgumentException: contentView required: pkg=com.octoplus.client id=1 notification=Notification(pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 kind=[null])
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2721)
at android.app.ActivityThread.access$1900(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: contentView required: pkg=com.octoplus.client id=1 notification=Notification(pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 kind=[null])
at android.os.Parcel.readException(Parcel.java:1435)
at android.os.Parcel.readException(Parcel.java:1385)
at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:320)
at android.app.NotificationManager.notify(NotificationManager.java:136)
at android.app.NotificationManager.notify(NotificationManager.java:109)
at com.octoplus.client.services.notif.sendNotif(notif.java:41)
at com.octoplus.client.services.notif.onStartCommand(notif.java:31)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2704)
... 10 more
How I can fix it?
我该怎么办呢?
1 个解决方案
#1
found that you are not calling Notification.setLatestEventInfo(). this might cause crash try uncommenting it
发现你没有调用Notification.setLatestEventInfo()。这可能导致崩溃尝试取消注释
#1
found that you are not calling Notification.setLatestEventInfo(). this might cause crash try uncommenting it
发现你没有调用Notification.setLatestEventInfo()。这可能导致崩溃尝试取消注释