Java 有没有办法通过编程来操纵飞机模式?安卓

Java 有没有办法通过编程来操纵飞机模式?安卓,java,android,mode,airplane,Java,Android,Mode,Airplane,我想打开或关闭im制作的节省电池应用程序的飞行模式。 真的没有办法吗 这是我目前的代码: package com.example.airplaneog; import android.content.Intent; import android.os.Bundle; import android.provider.Settings; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBarAc

我想打开或关闭im制作的节省电池应用程序的飞行模式。 真的没有办法吗

这是我目前的代码:

package com.example.airplaneog;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

    }

    @Override
    protected void onStart(){
        super.onStart();

        setContentView(R.layout.fragment_main);

        final Button testButton = (Button) findViewById(R.id.button1);

        if (getAPstate() == true){
            testButton.setText("Turn Airplane Mode OFF");           
        }else {

            testButton.setText("Turn Airplane Mode ON");
        }
    }

    public boolean getAPstate(){
        return Settings.System.getInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void apmodeonoff(View view){

        final Button testButton = (Button) findViewById(R.id.button1);

        boolean st;

        if (getAPstate() == true){
            testButton.setText("Turn Airplane Mode OFF");
            Settings.System.putBoolean(getContentResolver(),  Settings.System.AIRPLANE_MODE_ON, 0);
            st = false;


        }else {
            testButton.setText("Turn Airplane Mode ON");
            Settings.System.putBoolean(getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 1);
            st = true;
        }

         Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
         intent.putExtra("state",  st);
         sendBroadcast(intent);

    }

    /**
     * A placeholder fragment containing a simple view.
     */


    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

当我在android nexus 7上运行应用程序时,什么都没有发生。。。我做错了什么?

您应该使用
设置.System.putInt
而不是
设置.System.putBoolean

if (getAPstate() == true){
            testButton.setText("Turn Airplane Mode OFF");
            Settings.System.putInt(getContentResolver(),  Settings.System.AIRPLANE_MODE_ON, 0);
            st = false;


        }else {
            testButton.setText("Turn Airplane Mode ON");
            Settings.System.putInt(getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 1);
            st = true;
        }

这不是我希望应用程序在我的手机上玩弄的东西。从API级别17开始,这将不起作用。从Android 4.2开始,这将不起作用。最好先检查API级别,然后将用户带到设置屏幕,让他们手动切换模式。所以没有办法吗?我想做一个节省电池的应用程序,定期打开和关闭飞机模式