D Dimitrov
D Dimitrov

Reputation: 115

Starting a Service with Alarm after the app is closed

I am making a simple app that that consists of a service that pops a notification several times a day. I am trying to make this with a Alarm. All works perfect if I don't close the app. However, if I close the app when the receiver is triggered I get the message "the app has stopped working" or whatever.

I was wandering if it is because the service cannot work without an activity of the app or something? Any insights?

Here is my code:

The receiver:

public class AlarmReceiver extends BroadcastReceiver {
    public static final int REQUEST_CODE = 12345;
    public static final String ACTION = "com.ddimitrovd.hap4eapp4e.alarm";

    // Triggered by the Alarm periodically (starts the service to run task)
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, MainIntentService.class);
        i.putExtra("foo", "bar");
        context.startService(i);
    }
}

Here is the Service:

public class MainIntentService extends IntentService {

    int noteID = 1;
    String chanelID;

    public MainIntentService() {
        super("MainIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        chanelID = getString(R.string.channel_ID);
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();

        // Do the task here
        createNotificationChannel();
        popNotif();
    }

    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    }

    private void popNotif() {
        // Create an explicit intent for an Activity in your app
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, chanelID)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("My notification")
                .setContentText("Hello World!")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                // Set the intent that will fire when the user taps the notification
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

        notificationManager.notify(noteID, mBuilder.build());
    }

    private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.channel_name);
            String description = getString(R.string.channel_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(chanelID, name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }
}

I am certain the alarm triggers the receiver.

Thank you!

Upvotes: 2

Views: 301

Answers (1)

D Dimitrov
D Dimitrov

Reputation: 115

I think that I found the problem. I reckon that my IntentService can not execute complexly because the BroadcastReceiver process is killed before it can do it. More info here. What I did was simply to pop my notification in the receiver, but I guess a better solution would be to start another thread or to put it async.

Upvotes: 1

Related Questions