任何以编程方式操纵飞机模式的方法?安卓

时间:2014-05-12 20:40:38

标签: java android mode airplane

我想开启或关闭飞行模式,以便制作省电的应用程序。 有没有办法做到这一点?

这是我目前的代码:

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上运行应用程序时,没有任何反应......我做错了什么?

1 个答案:

答案 0 :(得分:0)

您应该使用Settings.System.putInt代替Settings.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;
        }