Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5939481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:50:04+00:00 2026-05-22T15:50:04+00:00

I took some code from some questions here in SO as well as some

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:50:05+00:00Added an answer on May 22, 2026 at 3:50 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this statement is some old code and it took me a second
From reading a few questions and answers here, it seems that a telnet stream
I recently took my Db initiating code out of the __construct of my Page
I have some basic questions around understanding fundamentals of Performance testing. I know that
I've moved on from trying to use OpenGL through Penumbra to trying to draw
I took a data structures class in C++ last year, and consequently implemented all
I took the plunge this afternoon and began studying LINQ, so far just mucking
It took me forever to reduce the problem to this. I cannot express the
This took me a while to figure out and I found the answer on
I wish to calculate the time it took for an API to return a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.