2

I'm trying to find a way to make my app launch an activity when a phone is unlocked/turned on after X time (15 minutes for example). I've been jotting ideas down and trying to find a way with no major errors.

I tried an IF command using

public void onReceive(Context context, Intent intent){
 KeyguardManager keyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);
 if (!keyguardManager.isKeyguardSecure()){
  Intent startup = new Intent(MainActivity.this, NextActivity.class);}

But the NextActivity gets re-opened immediately whenever closed. I haven't tried the timer addition yet because I've been stuck on this part for about 2 days but any advice going into it would be appreciated.

Also I'm going to add an If command before this command to check if the device even has a security lock, and if it doesn't the code will check to see if the screen is on, but I haven't gotten that far yet.

Sorry If I'm not clear on what I need help on, only ever browsed this website.

Connor A
  • 39
  • 2
  • 6

3 Answers3

0

You can listen to System Bootup Event and then start a timer to count for 15mins.

An answer about BOOTUP receive : BOOTUP

Community
  • 1
  • 1
Juliyanage Silva
  • 2,529
  • 1
  • 21
  • 33
0

You can use BroadCastReceiver to receive BOOT_COMPLETED broadcast. And then start the timer to count for X time (15 mins) Check out this link for more info. Boot Completed

You have to give the permission in manifest file like

< uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" >

Your Receiver should be like

<receiver
        android:name="BootReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

And BroadcastReceiver class will be like

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
          // start a timer to count for 15mins
          // ....
          // start your luncher activity
    }
}
Alex Roy
  • 120
  • 1
  • 12
0

Keep track of the last time your MainActivity went to the background. When MainActivity comes back to the foreground, check the time difference. If it's more than 15 minutes, display NextActivity.

For example, in your MainActivity

@Override
protected void onPause() {
    lastTick = System.currentTimeMillis();
}

@Override
protected void onResume() {
    if((System.currentTimeMillis() - lastTick) > 900000){//15 minutes
        Intent intent = new Intent(MainActivity.this, NextActivity.class);
        startActivityForResult(intent, 0);
    }
}
Short answer
  • 144
  • 7