Android:在服务中创建通知

时间:2012-12-23 15:15:51

标签: android service notifications

我在服务中创建通知并在状态栏中显示通知时遇到问题...

这是我在活动中创建通知的方式:

    int NOTIFY_ID=100;
    Intent notificationIntent = new Intent(this, Grafik.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.notification)
            .setContentTitle("Message")
            .setContentText("Hello world");
    NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mgr.notify(NOTIFY_ID, mBuilder.build());

但是,当我将此代码放入我的服务中时,它会崩溃,我认为问题就在这里

Intent notificationIntent = new Intent(this, Grafik.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

在这里

NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)

但我不知道为什么......

0 个答案:

没有答案