来自服务的通知兼容
notificationcompat from service
我正在使用来自 android 服务的 NotificationCompat 这是我的代码
public void giveNotification(){
task.cancel();
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder reminderNotification = new NotificationCompat.Builder(MyService.this);
reminderNotification.setContentTitle("Times up");
reminderNotification.setContentText("Application is over used");
reminderNotification.setSmallIcon(R.drawable.ic_launcher);
notificationManager.notify(0,reminderNotification.build());
}
我从 timerTask
调用了函数 giveNotification()
。代码似乎是正确的,但它抛出了 IllegalArgumentException()
.
我的代码有什么问题。
在 Gingerbread 及以下版本中,您必须设置内容意图,否则将抛出 IllegalArgumentException
。
所以以下应该可以解决您的问题:
//Required on Gingerbread and below
Intent notificationIntent = new Intent(this, MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
我正在使用来自 android 服务的 NotificationCompat 这是我的代码
public void giveNotification(){
task.cancel();
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder reminderNotification = new NotificationCompat.Builder(MyService.this);
reminderNotification.setContentTitle("Times up");
reminderNotification.setContentText("Application is over used");
reminderNotification.setSmallIcon(R.drawable.ic_launcher);
notificationManager.notify(0,reminderNotification.build());
}
我从 timerTask
调用了函数 giveNotification()
。代码似乎是正确的,但它抛出了 IllegalArgumentException()
.
我的代码有什么问题。
在 Gingerbread 及以下版本中,您必须设置内容意图,否则将抛出 IllegalArgumentException
。
所以以下应该可以解决您的问题:
//Required on Gingerbread and below
Intent notificationIntent = new Intent(this, MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);