来自服务的notificationcompat

时间:2015-04-18 07:47:53

标签: android service notifications

我在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());
}

我从giveNotification()调用了函数timerTask。代码似乎是正确的,但它正在抛出IllegalArgumentException()

我的代码有什么问题。

1 个答案:

答案 0 :(得分:2)

在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);