我正在使用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()

我的代码有什么问题。

最佳答案

在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 - 服务通知,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29714506/

10-10 07:09