2014-05-12 3 views
0

배터리 절약 앱 만들기를 위해 비행기 모드를 켜거나 끕니다. 정말 이렇게 할 방법이 없습니까? 난 내 안드로이드 넥서스 7에서 응용 프로그램을 실행할 때 내가 잘못 뭐하는 거지, 아무 일도 일어나지 않습니다 ...프로그래밍 방식으로 비행기 모드를 조작하는 방법은 무엇입니까? android

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; 
     } 
    } 

} 

: 여기

내 현재 코드인가?

+1

이것은 응용 프로그램이 내 전화에서 원숭이처럼 보이기를 바라는 것이 아닙니다. –

답변

0

Settings.System.putBoolean 대신 Settings.System.putInt을 사용해야합니다.

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; 
     } 
+0

API 레벨 17에서 작동하지 않습니다. – CommonsWare

+0

Android 4.2 이상에서는 작동하지 않습니다. 먼저 API 수준을 확인한 다음 사용자가 수동으로 모드를 전환하도록 설정 화면으로 이동하십시오. –

+0

그렇게 할 방법이 없습니까? 정기적으로 비행기 모드를 켜고 끄는 배터리 절약 응용 프로그램을 만들고 싶습니다. – Kelvin