0

我想为正在制作的省电应用打开或关闭飞行模式。真的没有办法做到这一点吗?

这是我当前的代码:

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 上运行应用程序时,什么也没有发生……我做错了什么?

4

1 回答 1

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;
        }
于 2014-05-12T20:48:51.570 回答