在后台自动注销应用程序

时间:2014-08-21 02:56:27

标签: android alarmmanager alarm

我创建了一个应用程序,将在1分钟内注销;当它在后台运行时,采用Alarm Mgr方法。但是,当我在基于Android的设备上调试和运行时,没有任何反应。我已经运行了一些基本的诊断测试,发现BaseActivity没有记录在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); 
        }
    }
}

3 个答案:

答案 0 :(得分:0)

我认为您需要致电registerReceiver - 您可以找到一些信息here

你应该在OnStart()方法中调用类似的东西:

context.registerReceiver(alarmMgr, alarmIntent);

编辑 - 我会这样做:

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 ));
    }
}

并确保在destroy方法中清理所有内容:

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

设置AlarmManager并在setupAlarm()方法中处理广播。在OnStop()方法中,实际上将警报设置为在60秒(60 * 1000毫秒)后关闭,此时它将广播到br(BroadcastReceiver),您可以运行注销逻辑。

编辑2 - 我已经在Galaxy S5上测试了这段代码并且效果很好。您可以找到代码here。所有你需要做的就是运行应用程序,按回家等待10秒钟,然后会出现一个吐司,说你已经退出。

答案 1 :(得分:0)

您可以在Activity.java或fragment.java文件中声明注销的时间段。

@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


}

答案 2 :(得分:0)

问题是AlarmManager无法找到要调用的broadcastreceiver。

将SampleBootReceiver写入不同文件并在清单中声明它的解决方案。除非在清单中声明,否则PendingIntent.getBroadcast将找不到它。声明如下(相应地更改包名称),

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

如下所示创建PendingBroadcast(将PendingIntent.FLAG_UPDATE_CURRENT添加为最后一个参数):

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);