【问题标题】:Android notification from service on AsyncTask class来自 AsyncTask 类服务的 Android 通知
【发布时间】:2013-11-01 11:15:41
【问题描述】:

我有一个运行后台的服务,并使用 BroadcastReceiver 和 AlarmManager 检查 URL 的日常 pagerank,如果发生任何变化,请通知用户。所以基本上在一个服务中我正在运行 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)

谁能帮我解决这个错误。 谢谢

【问题讨论】:

  • 第 147 行是什么? notificationManager2 为空?
  • 第 147 行是 notificationManager2.notify(notificationIdentifier, notification2);
  • NullPointerException 是最简单的例外,您可以找出原因。只需逐行调试。 progress 可能为空,而您调用 progress[0]....
  • notificationManager2 为空。我认为问题在于这条线。 NotificationManager notificationManager2 = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);

标签: android service android-asynctask notifications


【解决方案1】:

使用下面的

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多