安卓开发退出程序时询问是否确定退出

共有两种效果:

第一种效果图如图: 

安卓开发退出程序时询问是否确定退出_第1张图片

 

在MainActivity中加几句话即可:

AlertDialog builder=null;
  @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            builder = new AlertDialog.Builder(MainActivity.this)
                    .setTitle("温馨提示:")
                    .setMessage("您是否要退出程序?")
                    .setPositiveButton("确定",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                                    int whichButton) {
                                    MainActivity.this.finish();
                                }
                            })
                    .setNegativeButton("取消",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                                    int whichButton) {
                                    builder.dismiss();
                                }
                            }).show();
        }
        return true;
    }

第二种效果图如下

 

安卓开发退出程序时询问是否确定退出_第2张图片

这种我是用了广播去实现详情查看我的另一篇关于广播的博客,那篇里有强制下线的功能,在这里我就把强制下线用到了这里

首先用一个类去管理所有的activity


public class ActivityCollector {
    public static List activities = new ArrayList<>();
    public static void addActivity(Activity activity){
        activities.add(activity);
    }
    public static void removeActivity(Activity activity){
        activities.remove(activity);
    }
    public static void finishAll(){
        for (Activity activity : activities){
            if (!activity.isFinishing()){
                activity.finish();
            }
        }
        activities.clear();
    }
}

 然后在新建一个所有活动的父类(Receiver定义在其中,去执行命令)

public class BaseActivity extends AppCompatActivity {

    private OfflineReceiver offlineReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCollector.addActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("offline");
        offlineReceiver = new OfflineReceiver();
        registerReceiver(offlineReceiver,intentFilter);

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (offlineReceiver != null){
            unregisterReceiver(offlineReceiver);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }
    class OfflineReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, final Intent intent) {
            ActivityCollector.finishAll();
        }
    }
}

然后我们把所有活动都继承BaseActivity,BaseActivity继承AppCompatActivity

public class MainActivity extends BaseActivity
public class LoginActivity extends BaseActivity

最后在MainActivity中写 

@Override
    public void onBackPressed() {
        exit();
    }
     private long presstime = 0L;
    private void exit() {
       if (System.currentTimeMillis()-presstime > 2000){
           Toast.makeText(MainActivity.this,"再按一次退出程序(#^.^#)", Toast.LENGTH_SHORT).show();
           presstime = System.currentTimeMillis();
           Log.e("mainactivity",""+System.currentTimeMillis());
       }else{
           Intent intent = new Intent("offline");
           sendBroadcast(intent);
       }
    }

 

 

 

 

 

 

 

你可能感兴趣的:(Android)