0

私はAndroidに非常に慣れていないので、おそらく些細な問題だと思うことに頭を悩ませてきました-チュートリアルやその他のStackOverflowの質問を見てきましたが、役に立ちませんでした.

IntentService 内に Intent を作成しようとしていますが、下の行マーカー ERROR で常に NullPointerException を取得します (sendNotification メソッド内)。

私はさまざまに試しました:

インテント インテント = 新しいインテント(getApplicationContext(), TestActivity.class);

インテント インテント = 新しいインテント(これ、TestActivity.class);

インテント インテント = 新しいインテント(SQLHealthService.this, TestActivity.class);

Intent を作成しようとすると、常に null ポインター例外が発生します。

    public class SQLHealthService extends IntentService {
.
.
.
    public SQLHealthService(String url) {
        super("SQLHealth Service");
        // TODO Auto-generated constructor stub
        this.wsURL = "http://demo9138157.mockable.io/";
        serverList = new ArrayList<Server>();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    Log.i(Constants.LOG_TAG, "SQLHealthService invoked");
    getServers();
    }

    List<Server> getServers() {
    AsyncHttpClient client = new AsyncHttpClient();
    client.get(wsURL, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            try {
                .
                                    .
                                    .
                    sendNotification(notify);
            } catch (Exception e) {
                System.out.println("Could not create json object");
                System.out.println(e);
            }

            // System.out.println(response);
        }
         });

         return serverList;
      }

    private void sendNotification(int notify) {
    try {

    Intent intent = new Intent(getApplicationContext(), TestActivity.class); <-- ERROR
    intent.putExtra(Constants.FORCE_RELOAD, true);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            intent, 0);

    Notification mNotification = new Notification.Builder(this)
    .setContentTitle("SQL Server issues detected")
    .setContentText(notify + " SQL Servers with issues have been detected")
    .setSmallIcon(R.drawable.database)
    .setContentIntent(contentIntent)
    .addAction(R.drawable.erase_database, "View", contentIntent)
    .addAction(0, "Remind", contentIntent)
    .build();

    NotificationManager mgr = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    mNotification.flags |= Notification.FLAG_AUTO_CANCEL;

    mgr.notify(0, mNotification);
    } catch (Exception e) {

    System.out.println(e);
    }
}

編集:明確にするために、私が達成しようとしていることについて言及する必要があります。AlarmManager を使用して、アプリを 10 分ごとに「起動」させ、REST Web サービスを呼び出し、特定の条件が満たされた場合に通知を送信しようとしています。

SQLHealthService が呼び出されます

public class SQLHealthAlarmReceiver extends BroadcastReceiver {
      // onReceive must be very quick and not block, so it just fires up a Service
       @Override
       public void onReceive(Context context, Intent intent) {
          Log.i(Constants.LOG_TAG, "SQLHealthAlarmReceiver invoked, starting SQLHealthService");
          context.startService(new Intent(context, SQLHealthService.class));
       }
}
4

1 に答える 1

1

IntentServiceは、応答を待つようには設計されていません。から戻るとすぐonHandleIntentに、キューに意図がなくなった場合、サービスは強制終了されます (この背後にある理由については、ドキュメントを参照してください)。したがって、 に到達するsendNotificationと、コンテキストは存在しません。

于 2014-05-05T19:07:15.283 に答える