Search code examples
javaandroidandroid-serviceandroid-alarms

Android Alarm Service Not Responding


I took some code from some questions here in SO as well as some other website and I came up with a reasonable solution.

What I am trying to do: I need to shutdown the app after 2 minutes of inactivity. So The idea is to start up the alarm service when our app goes in into 'onPause' and cancel the service when our app goes into 'onResume'.

What I currently Have: Here is the relevant code that is currently running. My issue is that the TimeoutService java file is never being 'onCreated'. Does simply calling AlarmManager.set NOT start up the pending intent?

The Timeout.java File

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class Timeout 
{
    private static final int REQUEST_ID = 0;
    private static final long DEFAULT_TIMEOUT = 2 * 60 * 1000;  // 2 minutes

    public static final String INTENT_TIMEOUT = "timeoutintent";

    public static void start(Context ctx) 
    {
        //long triggerTime = System.currentTimeMillis() + DEFAULT_TIMEOUT;
        long triggerTime = System.currentTimeMillis() + (5000);
        AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(ctx, TimeoutService.class);
        PendingIntent pi = PendingIntent.getBroadcast(ctx, REQUEST_ID, intent, 0);
        am.set(AlarmManager.RTC, triggerTime, pi);
    }

    public static void cancel(Context ctx) 
    {
        AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(ctx, TimeoutService.class);
        PendingIntent pi = PendingIntent.getBroadcast(ctx, REQUEST_ID, intent, 0);
        am.cancel(pi);
    }

}

LockingActivity File. This is used as a superclass to all of my Activities.

import android.app.Activity;
import android.widget.Toast;

public class LockingActivity extends Activity
{
    @Override
    protected void onPause() 
    {
        super.onPause();
        Timeout.start(this);
    }

    @Override
    protected void onResume() 
    {
        super.onResume();
        Timeout.cancel(this);
        checkShutdown();
    }

    private void checkShutdown() 
    {
        if ( AppVM.isShutDown() )
        {
            Toast.makeText(this, "Shuting Down", 1).show();
            finish();
        }
    }
}

I could send over the TimeoutService file as well, but it's just a typical service file. The problem is the TimeoutService class is never being instanced, so I can't imagine the problem would lie in that class.


Solution

  • I think you are complicating things with an alarm. Use a Handler and postdelayed() to set a Runnable in two minutes, all in your main activity. Any user interaction cancels the post and sets a new one for the next two minutes. The runnable needs only yourActivity.finish();

    Follow this answer here: Count for 45 seconds, pause for 20 then repeat with different title for an example of a timer and how to remove the callbacks.