来自AsyncTask类服务的Android通知

时间:2013-11-01 11:15:41

标签: android service android-asynctask notifications

我有一个运行后台的服务,并使用BroadcastReceiver和AlarmManager检查URL的每日页面,如果发生任何更改,请通知用户。所以基本上在服务中我正在运行asynctask类并检查urls pagerank并向用户发送通知。

以下是代码:

public class PagerankCheck extends AsyncTask<String, Integer, Integer>{
    private android.content.Context servicecontext;
    private String uri = "";

 public PagerankCheck(android.content.Context context, String url) {
        servicecontext = context;
        uri = url;
    }

protected void onProgressUpdate(Integer... progress) {
    int icon = R.drawable.ic_launcher;
    CharSequence ticker = uri + " pagerank changed from " + oldPR + " to " + progress[0].toString();
    long showAt = System.currentTimeMillis(); 
    CharSequence notificationTitle = "Notification:";
    CharSequence notificationMessage = "Test.";
    final int notificationIdentifier = 101;
    NotificationManager notificationManager2 =
    (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
    Notification notification2 = new Notification(icon, ticker, showAt);
    android.content.Intent intent = new android.content.Intent();
    PendingIntent pendingIntent2 = PendingIntent.getActivity(servicecontext, 0, intent, 0);
    notification2.setLatestEventInfo(servicecontext,  notificationTitle, notificationMessage, pendingIntent2);
    notificationManager2.notify(notificationIdentifier, notification2);
}

执行最后一行代码时会在LogCat上产生以下错误。

11-01 07:14:26.488: E/AndroidRuntime(851): FATAL EXCEPTION: main
11-01 07:14:26.488: E/AndroidRuntime(851): java.lang.NullPointerException
11-01 07:14:26.488: E/AndroidRuntime(851):  at org.siteprice.googlepagerankchecker.PagerankCheck.onProgressUpdate(PagerankCheck.java:147)
11-01 07:14:26.488: E/AndroidRuntime(851):  at org.siteprice.googlepagerankchecker.PagerankCheck.onProgressUpdate(PagerankCheck.java:1)
11-01 07:14:26.488: E/AndroidRuntime(851):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647)
11-01 07:14:26.488: E/AndroidRuntime(851):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 07:14:26.488: E/AndroidRuntime(851):  at android.os.Looper.loop(Looper.java:137)
11-01 07:14:26.488: E/AndroidRuntime(851):  at android.app.ActivityThread.main(ActivityThread.java:5103)
11-01 07:14:26.488: E/AndroidRuntime(851):  at java.lang.reflect.Method.invokeNative(Native Method)
11-01 07:14:26.488: E/AndroidRuntime(851):  at java.lang.reflect.Method.invoke(Method.java:525)
11-01 07:14:26.488: E/AndroidRuntime(851):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-01 07:14:26.488: E/AndroidRuntime(851):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-01 07:14:26.488: E/AndroidRuntime(851):  at dalvik.system.NativeStart.main(Native Method)

任何人都可以帮我解决这个错误。 感谢

1 个答案:

答案 0 :(得分:0)

使用以下

NotificationManager notificationManager2 =
(NotificationManager) servicecontext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);