Solver
Solver

Reputation: 51

Getting data to a service without restarting it

I have a service that sends a notification at a random time, telling me to press a button. This button needs to be pressed quickly because after 2 minutes it will disappear again. But after those 2 minutes I don't know how I can see if the button has or hasn't been pressed.

Somehow I need to get something like a boolean from my MainActivity to my service, but I don't believe I can do that with an intent because then I would restart my service.

I have looked for an answer but wasn't able to find a solution, any help will be much appreciated!

My service:

`package com.example.pressme_alpha;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

public class ButtonAlarmService extends IntentService{

private static final String INTENT_NAME = "notification";

private NotificationManager nm;
private Notification notification;

public ButtonAlarmService() {
    super("Imma button!");
}

@SuppressWarnings("static-access")
@Override
protected void onHandleIntent(Intent intent) {
    nm = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);

    Intent newIntent = new Intent(this.getApplicationContext(), MainActivity.class);
    newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    newIntent.putExtra(INTENT_NAME, true);
    PendingIntent pendingIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, newIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this);
    notification = notifBuilder.setContentIntent(pendingIntent).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Press me - alpha").setContentText("You need to press the button!").build();

    notifBuilder.setAutoCancel(true);
    nm.notify(0, notification);
    startActivity(newIntent);

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    //Check if the button is pressed here

    ButtonAlarmReceiver.completeWakefulIntent(intent);
}

} `

Upvotes: 0

Views: 179

Answers (1)

Goran Horia Mihail
Goran Horia Mihail

Reputation: 3645

Extending Service (not IntentService) is what you want to do as it will keep runing untill you explicitly tell it to stop via stopService(Intent) method or if the service calls stopSelf() on its self. You can send signals to the service via startService(Intent) method. This will start the service the first time its called (when the service is not running) and just send data to it if called subsequent times.

Make sure to spawn a new thread if you are doing heavy proccessing in the service as this will run on the Main thread (or UI thread depending on what you want to call it). You do not want to block the main thread.

Upvotes: 4

Related Questions