Auto Logout of App when in Background

喜你入骨 提交于 2019-12-02 09:45:29

You can declare time period for logout in your Activity.java or in your fragment.java file.

@Override
public void onStart() {
    super.onStart();

     new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                finish(); // To CLOSE YOUR APP IN ACTIVITY

               ///LOG OUT CODE HERE


            }
        }, 1800000 ); //HAlf hour -1800000


}

The problem is AlarmManager is not able to find the broadcastreceiver to invoke.

Solution to write SampleBootReceiver in different file and declare it in manifest. PendingIntent.getBroadcast will not find it unless it is declare in manifest. Declare it as below (Change packagename accordingly),

<receiver android:name="com.app.packagename.SampleBootReceiver"></receiver>

Create PendingBroadcast as below (add PendingIntent.FLAG_UPDATE_CURRENT as last param):

Intent intent= new Intent(context, SampleBootReceiver.class);
PendingIntent alarmIntent=PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1 * 60 * 1000), alarmIntent);

I think you need to call registerReceiver - you can find some info here

You should call something like this inside your OnStart() method:

context.registerReceiver(alarmMgr, alarmIntent);

EDIT - I would do something like this:

public class BaseActivity extends Activity {
    BaseActivity context;
    private AlarmManager alarmMgr; //TO CALL OUT THE CLASS OF THE ALARM SERVICE
    private PendingIntent alarmIntent; // FOR TARGET FUNCTION TO PERFORM WITH BROADCASTRECEIVER
    private BroadcastReceiver br;

    BaseActivity(){
        context=this;
    }    

    @Override
     protected void onStart(){

         Log.i("RootActivity:SampleBootReceiver", "On Timeout after 1 min");    
         super.onStart();// CALLING ON SUPER CLASS METHOD OF ALARM MGR    
         setupAlarm(); // this could be called in onCreate() instead    
    }

    @Override
    protected void onStop() {
         super.onStop();
         alarmMgr.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + SIXTY_SECONDS, alarmIntent );
    }

    private void setupAlarm() {
          br = new BroadcastReceiver() {
                @Override
                public void onReceive(Context c, Intent i) {
                        // DO YOUR LOGOUT LOGIC HERE
                        Toast.makeText(c, "You are now logged out.", Toast.LENGTH_LONG).show();
                }
            };
          registerReceiver(br, new IntentFilter("com.myapp.logout") );
          alarmIntent = PendingIntent.getBroadcast( this, 0, new Intent("com.myapp.logout"),0);
          alarmMgr = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
    }
}

And make sure you clean everything up in the destroy method:

@Override
protected void onDestroy() {
       alarmMgr.cancel(alarmIntent);
       unregisterReceiver(br);
       super.onDestroy();
}

Setting up the AlarmManager and the broadcasting is handled in the setupAlarm() method. In the OnStop() method, you actually set the alarm to go off after 60 seconds (60 * 1000 milliseconds), at which point it broadcasts to the br (BroadcastReceiver) and you can run your logout logic.

EDIT 2 - I have tested out this code on a Galaxy S5 and it worked nicely. You can find the code here. All you have to do is run the app, press home and wait for 10 seconds and a toast will appear saying you have been logged out.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!