Question

I'm very new to Android and I've been banging my head against the wall on what I believe is probably a trivial problem - I've looked through tutorials and other StackOverflow questions but to no avail.

I'm trying to create an Intent inside of an IntentService but constantly get NullPointerException on the line marker ERROR below (in the sendNotification method).

I've variously tried:

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

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

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

to no avail, always yielding a null pointer exception when trying to create the 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);
    }
}

EDIT: For clarity I should mention what I'm trying to accomplish. I'm attempting to use the AlarmManager to have the app 'wake up' every 10 minutes, make a call to a REST web service and then if certain conditions are met send a notification.

SQLHealthService is called from

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));
       }
}
Was it helpful?

Solution

IntentService is not designed to wait for a response. As soon as you go back from onHandleIntent, if there is no intent in the queue any more the service is killed (look at the documentation for the reasoning behind this). So when you get to sendNotification, no context exist.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top