应用程序的自动退出的背景时, [英] Auto Logout of App when in Background

查看:176
本文介绍了应用程序的自动退出的背景时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个应用程序,它会在1注销敏;当它在后台运行,通过使用报警经理方法。然而,当我调试和基于Android的设备上运行的,没有任何反应。我已经运行一些基本的诊断测试,并发现BaseActivity没有被记录在logcat中。

 公共类BaseActivity延伸活动{

    BaseActivity环境;
    私人AlarmManager alarmMgr; //调出报警服务类
    私人PendingIntent alarmIntent; //对于性能指标的执行与BroadcastReceiver的

    BaseActivity(){
        上下文=这一点;
    }


    @覆盖
     保护无效的OnStart(){

         Log.i(RootActivity:SampleBootReceiver,关于超时1分钟后);

         super.onStart(); //调用报警MGR超类的方法

   如果(alarmMgr!= NULL){
          alarmMgr.cancel(alarmIntent);
             //在启动条件,报警VAL IS NOT NULL,将中止意图,别人会

   开始()

         }

 }

 //类时调用的应用程序是无法再看到用户(运作在背景中)

  @覆盖

   保护无效的onStop(){

    super.onStop();

    alarmMgr =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

    意向意图=新的意图(背景下,SampleBootReceiver.class);

    alarmIntent = PendingIntent.getBroadcast(上下文,0,意图,0);

    alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime()+ 60000,alarmIntent);
        //附表报警1分钟
    }
    公共类SampleBootReceiver扩展的BroadcastReceiver {
        //报警器一旦推出,便登出APP
        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){
            //注销服务
            Log.i(RootActivity:SampleBootReceiver,关于超时1分钟后);
            意图int​​ent_login =新的意图(背景下,MainActivity.class);
            //关闭所有其他活动,并把活动正在展开顶端
            intent_login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent_login);
            // stopServiceAndLogout();
            完();
        }
    }
    公共无效cancelAlarm(上下文的背景下){
        // UPON激活的APP,取消报警(如果设置),以确保注销将不会被执行
        如果(alarmMgr!= NULL){
            alarmMgr.cancel(alarmIntent);
        }
    }
}
 

解决方案

我想你需要调用 registerReceiver - 你可以找到一些信息<一个href="http://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver,%20android.content.IntentFilter)"相对=nofollow>这里

您应该呼吁这样的事你里面的OnStart()方法:

  context.registerReceiver(alarmMgr,alarmIntent);
 

修改 - 我会做这样的事情:

 公共类BaseActivity延伸活动{
    BaseActivity环境;
    私人AlarmManager alarmMgr; //调出报警服务类
    私人PendingIntent alarmIntent; //对于性能指标的执行与BroadcastReceiver的
    私人BroadcastReceiver的BR;

    BaseActivity(){
        上下文=这一点;
    }

    @覆盖
     保护无效的OnStart(){

         Log.i(RootActivity:SampleBootReceiver,关于超时1分钟后);
         super.onStart(); //调用报警MGR超类的方法
         setupAlarm(); //这可以称为的onCreate(),而不是
    }

    @覆盖
    保护无效的onStop(){
         super.onStop();
         alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime()+ SIXTY_SECONDS,alarmIntent);
    }

    私人无效setupAlarm(){
          BR =新的BroadcastReceiver(){
                @覆盖
                公共无效的onReceive(上下文C,意图我){
                        //做你的注销这里的逻辑
                        Toast.makeText(C,你现在已经退出,Toast.LENGTH_LONG).show();
                }
            };
          registerReceiver(BR,新的IntentFilter(com.myapp.logout));
          alarmIntent = PendingIntent.getBroadcast(此,0,新意图(com.myapp.logout),0);
          alarmMgr =(AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));
    }
}
 

和确保清洁一切行动的破坏方法:

  @覆盖
保护无效的onDestroy(){
       alarmMgr.cancel(alarmIntent);
       unregisterReceiver(BR);
       super.onDestroy();
}
 

设置的AlarmManager和广播在setupAlarm()方法处理。在的onStop()方法,您实际设置报警60秒(60 * 1000毫秒)后熄灭,此时它广播到BR(BroadcastReceiver的),你可以运行你的注销逻辑。

编辑2 - 我已经测试了这code在银河S5和它的工作很好。你可以找到code 这里。所有你需要做的就是运行应用程序,preSS回家,等待10秒钟,举杯会出现说你已退出。

I Have created an app which will log out within 1 Min; when it is running in the background, by employing Alarm Mgr method. However, when I debug and run in on Android based device, nothing happens. I have run some basic diagnostic test and found out that BaseActivity is not being logged in the Logcat.

  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

    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

   if(alarmMgr != null){    
          alarmMgr.cancel(alarmIntent);
             // IN START CONDITION, VAL OF ALARM IS NOT NULL, WILL ABORT INTENT, ELSE WILL

   START()

         }

 }

 // CLASS IS CALLED WHEN APP IS NO LONGER VISIBLE TO USER (FUNCTIONING IN THE BACKGROUND)

  @Override

   protected void onStop(){

    super.onStop();

    alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

    Intent intent= new Intent(context, SampleBootReceiver.class); 

    alarmIntent=PendingIntent.getBroadcast(context, 0, intent, 0); 

    alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime()+60000, alarmIntent);
        //SCHEDULE ALARM TO 1 MIN
    }
    public class SampleBootReceiver extends BroadcastReceiver{
        // ONCE THE ALARM IS LAUNCHED, WILL LOGOUT THE APP
        @Override
        public void onReceive(Context context, Intent intent){
            //LOGOUT OF THE SERVICE
            Log.i("RootActivity:SampleBootReceiver", "On Timeout after 1 min");
            Intent intent_login= new Intent(context, MainActivity.class);
            //CLOSE ALL OTHER ACTIVITIES AND BRING THE ACTIVITY BEING LAUNCHED TO THE TOP
            intent_login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent_login);
            //stopServiceAndLogout();
            finish();
        }
    }
    public void cancelAlarm(Context context){
        //UPON REACTIVATION OF APP, CANCEL THE ALARM(IF SET) TO ENSURE LOGOUT WILL NOT BE EXECUTED
        if(alarmMgr!=null){
            alarmMgr.cancel(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.

这篇关于应用程序的自动退出的背景时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆