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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With