android - 来自服务的通知兼容

标签 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());
}

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

我的代码有什么问题。

最佳答案

在 Gingerbread 及以下版本中,您必须设置内容 Intent ,否则将抛出 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/

相关文章:

android - Intent 过滤器数据路径中的哈希符号

android - android工具栏中的Searchview

ssh - 在 Windows 10 上启动 ssh-agent 失败 : "unable to start ssh-agent service, error :1058"

linux - 如何配置 systemd 服务以定期重启?

Angular4 尝试比较 '[object Object]' 时出错

notifications - 在GitLab中通知所有组成员失败的管道

python - Gtk 通知不显示正文持有 '<'

android - 在Android中启动第三方应用程序的安装

android - Android,Gradle:通过buildType名称设置testBuildType

javascript - 解析云代码 : Can you pass objects in a push notification?