我正在编写一个minSdkVersion = 9
和maxSdkVersion = 21
的应用。在service
我写了一个代码来发送通知,当预先设定的时间与当前时间匹配时,将通知用户。
但是,如果我在 API = 19 的模拟器中运行它,它会完全通知我但是如果我在我的设备中运行 API = 10 它没有通知我崩溃了应用程序。
我在Ubuntu中使用Android-Studio,但它没有提供设备调试功能。所以我甚至看不到logcat
。
这是我的相关代码
@Override
public int onStartCommand(Intent intent, int flags, int startId){
// Intent alarmIntent = new Intent(getBaseContext(), TRAlarmScreen.class);
// alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// alarmIntent.putExtras(intent);
// getApplication().startActivity(alarmIntent); */
Bundle extras = intent.getExtras();
int id = extras.getInt("id");
String title = extras.getString("title");
String des = extras.getString("des");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle(title);
mBuilder.setContentText(des);
mBuilder.setTicker("New Message Alert!");
mBuilder.setSmallIcon(R.drawable.tr);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(id, mBuilder.build());
return START_STICKY;
}
问题是什么?我是否需要添加任何支持库或其他东西才能使其在较低的API级别下工作?
错误是
java.lang.IllegalArgumentException:需要contentIntent:pkg = com.android.saathi id = 1 notification = Notification(vibrate = null,sound = null,defaults = 0x0,flags = 0x0)
我的logcat
在有api 10的设备中运行
02-27 02:27:41.812 4760-4760/com.android.saathi E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start service com.android.saathi.TRService@405a3bd0 with Intent { flg=0x4 cmp=com.android.saathi/.TRService (has extras) }: java.lang.IllegalArgumentException: contentIntent required: pkg=com.android.saathi id=1 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x0)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2372)
at android.app.ActivityThread.access$2800(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1111)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4277)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: contentIntent required: pkg=com.android.saathi id=1 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x0)
at android.os.Parcel.readException(Parcel.java:1326)
at android.os.Parcel.readException(Parcel.java:1276)
at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:394)
at android.app.NotificationManager.notify(NotificationManager.java:111)
at android.app.NotificationManager.notify(NotificationManager.java:91)
at com.android.saathi.TRService.onStartCommand(TRService.java:42)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2355)
at android.app.ActivityThread.access$2800(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1111)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4277)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
在setContentIntent()
上致电NotificationCompat.Builder
,说明当用户点击Notification
时会发生什么。
答案 1 :(得分:0)
我想我有同样的错误。
你需要在setContentIntent()
中放置一个PendingIntent(就像所选答案所说的那样)
在你的情况下是这样的:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle(title);
mBuilder.setContentText(des);
mBuilder.setTicker("New Message Alert!");
mBuilder.setSmallIcon(R.drawable.tr);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(id, mBuilder.build());