1:前提手机必须root,4.2之后官方明确说明只有系统app可以获取飞行模式权限  (把APP当作系统app是一种方案,但是这样太麻烦,果断pass,下面代码是直接用shell命令去发送广播设置 )

PS:研究之前我在网上也找了一些资料,发现大多数都是误导人的,还是用普通发送广播形式,这个是行不通的,下面贴代码

public class MainActivity extends AppCompatActivity
{

    @Bind(R.id.btn)
    Button btn;
    @Bind(R.id.btnOff)
    Button btnOff;
    private AudioManager am;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Snackbar.make(view, "飞行模式测试", Snackbar.LENGTH_LONG).setAction("Action", null).show();
            }
        });
        upgradeRootPermission(getPackageCodePath());
        am = (AudioManager) getSystemService(Service.AUDIO_SERVICE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @OnClick({R.id.btn, R.id.btnOff})
    public void clickListener(View view)
    {
        switch (view.getId())
        {
            case R.id.btn:
                on(null);
                Toast.makeText(this,"飞行模式开启",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btnOff:
                off(null);
                Toast.makeText(this,"飞行模式关闭",Toast.LENGTH_SHORT).show();
                break;
        }
    }

    /**
     * 设置手机飞行模式
     *
     * @param context
     * @param enabling true:设置为飞行模式 false:取消飞行模式
     */
    public static void setAirplaneModeOn(Context context, boolean enabling)
    {
        Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, enabling ? 1 : 0);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", enabling);
        context.sendBroadcast(intent);
    }

    /**
     * 判断手机是否是飞行模式
     *
     * @param context
     * @return
     */
    public static boolean getAirplaneMode(Context context)
    {
        int isAirplaneMode = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
        return (isAirplaneMode == 1) ? true : false;
    }

    /**
     * 应用程序运行命令获取 Root权限,设备必须已破解(获得ROOT权限)
     *
     * @return 应用程序是/否获取Root权限
     */
    public static boolean upgradeRootPermission(String pkgCodePath)
    {
        Process process = null;
        DataOutputStream os = null;
        try
        {
            String cmd = "chmod 777 " + pkgCodePath;
            process = Runtime.getRuntime().exec("su"); // 切换到root帐号
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(cmd + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e)
        {
            return false;
        } finally
        {
            try
            {
                if (os != null)
                {
                    os.close();
                }
                process.destroy();
            } catch (Exception e)
            {
            }
        }
        return true;
    }


    public static boolean off(String pkgCodePath)
    {
        Process process = null;
        DataOutputStream os = null;
        try
        {
            String cmd = "settings put global airplane_mode_on 0";
            String sendMsg="am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false";
            process = Runtime.getRuntime().exec("su"); // 切换到root帐号
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(cmd + "\n");
            os.writeBytes(sendMsg + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e)
        {
            return false;
        } finally
        {
            try
            {
                if (os != null)
                {
                    os.close();
                }
                process.destroy();
            } catch (Exception e)
            {
            }
        }
        return true;
    }

    public static boolean on(String pkgCodePath)
    {
        Process process = null;
        DataOutputStream os = null;
        try
        {
            String cmd = "settings put global airplane_mode_on 1";
            String sendMeg="am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true";
            process = Runtime.getRuntime().exec("su"); // 切换到root帐号
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(cmd + "\n");
            os.writeBytes(sendMeg + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e)
        {
            return false;
        } finally
        {
            try
            {
                if (os != null)
                {
                    os.close();
                }
                process.destroy();
            } catch (Exception e)
            {
            }
        }
        return true;
    }
}
//主要是on  off函数
需要添加权限,android.permission.WRITE_SECURE_SETTINGS
 


  • xxx

             好好好

  • 
    
    	
    色迷迷 哭 呕吐 大笑 口水 微笑 啵一个 发怒

    Hi,您需要填写昵称和邮箱!

    • 必填项
    • 必填项