zoukankan      html  css  js  c++  java
  • android使用广播退出应用程序

    由于在(Widget或Service、BroadcastReceiver中)使用startActivity()方法启动activity时需使用FLAG_ACTIVITY_NEW_TASK flag,所以在BaseActivity中使用setresult标记退出时不能完全退出应用程序(只能关闭通过广播打开的activity以及之后打开的activity)。

      因此本文设计使用广播退出应用程序:

    1、在退出事件中发送退出广播

    Intent intent = new Intent();
    intent.setAction("com.android.exitapp");
    baseActivity.sendBroadcast(intent);

    2、在BaseActivity中注册广播

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

      @Override
      public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if (intent.getAction().equals("com.android.exitapp")) {
          finish();
        }
      }
    };

    @Override
    protected void onResume() {
      // TODO Auto-generated method stub
      super.onResume();
      IntentFilter filter = new IntentFilter();
      filter.addAction(SystemConst.EXIT_APP);
      this.registerReceiver(this.broadcastReceiver, filter);
    }

    通过此法就可以退出所有activity了。

  • 相关阅读:
    Nginx:rewrite / if / return / set 和变量 (转载)
    【Aming】win10用anaconda3安装 TensorFlow
    git
    webpack(3)
    webpack(2)
    webpack(1)
    json
    Aliyun搭建svn服务器外网访问报错权限配置失败错误
    阿里云ECS服务器,mysql无法外网访问
    mysql出现 Unknown column 'Password' in 'field list'
  • 原文地址:https://www.cnblogs.com/diyishijian/p/4638059.html
Copyright © 2011-2022 走看看