0

我对 Android 很陌生,我一直在努力解决我认为可能是一个微不足道的问题——我已经查看了教程和其他 StackOverflow 问题,但无济于事。

我正在尝试在 IntentService 内部创建一个 Intent,但在下面的行标记 ERROR 上不断获得 NullPointerException(在 sendNotification 方法中)。

我已经尝试过:

Intent intent = new Intent(getApplicationContext(), TestActivity.class);

Intent intent = new Intent(this, TestActivity.class);

Intent intent = new Intent(SQLHealthService.this, TestActivity.class);

无济于事,在尝试创建 Intent 时总是产生空指针异常。

    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 回答