How Do I Disable AIRPLANE MODE tablet supports 4.4.2

前端 未结 2 405
挽巷
挽巷 2020-12-18 16:50

I have to disable AIRPLANE MODE and DATE TIME SETTINGS in my app, for that i used these intent filters but

2条回答
  •  一整个雨季
    2020-12-18 17:30

    TLDR: Don't do that.

    Think what happens if user couldn't enable airplane mode though if they want/should.
    Important system settings cannot allowed to change from your app.

    I recommend:

    1. Check airplane mode is disabled or not
    2. If enabled, prompt "this app is not work on airplane mode. open system settings app?" with "yes/cancel" dialog
    3. Close your app and/or open system settings app

    EDIT: it looks like this:

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void checkAirplaneMode() {
        int mode;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Log.v(TAG, "work with new api.");
            mode = Settings.Global.getInt(getActivity().getContentResolver(),
                    Settings.Global.AIRPLANE_MODE_ON, 0);
        } else {
            Log.v(TAG, "work with old api.");
            mode = Settings.System.getInt(getActivity().getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON, 0);
        }
        Log.v(TAG, "airplane mode: " + mode);
    
        if(mode == 1 /* airplane mode is on */) {
            new AlertDialog.Builder(getActivity()).setTitle("Sorry!")
                    .setMessage("This app doesn't work with Airplane mode.\n"
                            + "Please disable Airplane mode.\n")
                    .setPositiveButton("Open Settings", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            startActivity(new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS));
                            getActivity().finish();
                        }
                    })
                    .setNegativeButton("Close app", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            getActivity().finish();
                        }
                    })
                    .show();
        }
    }
    

    here's some point.

    • Check Build.VERSION.SDK_INT to use Settings.Global for newer devices. Airplane mode is moved to here.
    • If mode == 1, device is in airplane mode.
    • Open airplane mode setting Activity by
      startActivity(new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS));

提交回复
热议问题