Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set alarm when mobile has been idle using android?

Hi I want to set alarm when the phone hasn't been touched. If the screen hasn't been touched for nearly 2 minutes, the alarm sound would be raised. How can I do this? Can anybody help me? Thanks in advance.

like image 988
malavika Avatar asked Jan 24 '12 10:01

malavika


People also ask

How do I use alarm Manager on Android?

Then, to trigger an alarm, use the following (for instance in your main activity): AlarmManager alarmMgr = (AlarmManager)getSystemService(Context. ALARM_SERVICE); Intent intent = new Intent(this, MyAlarmReceiver. class); PendingIntent pendingIntent = PendingIntent.

Where is the alarm on my Android phone?

A standard alarm on an Android device is usually found in the Clock application. Open the App Drawer by swiping up on your phone, then select the Clock icon. Make sure Alarm is selected on the bottom left, then select the plus (+) sign. Pick the time you want your alarm to go off, then select OK.

What are alarms in Android?

Alarms in Android have the following characteristics: Alarms let you send intents at set times or intervals. You can use alarms with broadcast receivers to start services and perform other operations.


1 Answers

Pass the AlarmService through out the below code. This will find how long your device has been in idle.

idle.java

Handler hl_timeout = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try{
        hl_timeout.postDelayed(DoOnTimeOut, 15000);
        }catch(Exception e)
        {
            e.printStackTrace();
        }
}

//  Toast
Thread DoOnTimeOut = new Thread() {
    public void run() {
        try{
            Toast.makeText(getApplicationContext(), "System is idle", Toast.LENGTH_LONG).show();
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
};

@Override
public void onUserInteraction()
{
    super.onUserInteraction();
    //Remove any previous callback
    try{
    hl_timeout.removeCallbacks(DoOnTimeOut);
    hl_timeout.postDelayed(DoOnTimeOut, 15000);
    }catch(Exception e)
    {
        e.printStackTrace();
    }
}

Hope this helps you.

like image 69
Praveenkumar Avatar answered Oct 22 '22 22:10

Praveenkumar