飞行模式

来源:互联网 发布:联通宽带网络客服电话 编辑:程序博客网 时间:2024/04/27 22:28

开启/关闭/监听 飞行模式开启/关闭/监听 飞行模式

具体实现结合如下图片:


[java] view plain copy
  1. package lab.sodino.airplane;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Calendar;  
  4. import android.app.Activity;  
  5. import android.content.BroadcastReceiver;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.content.IntentFilter;  
  9. import android.os.Bundle;  
  10. import android.provider.Settings;  
  11. import android.util.Log;  
  12. import android.view.View;  
  13. import android.widget.Button;  
  14. import android.widget.TextView;  
  15. import android.widget.Toast;  
  16. /** 
  17.  * @author Sodino Email:sodinoopen@hotmail.com 
  18.  * */  
  19. public class AirPlaneAct extends Activity {  
  20.     private TextView txtInfo;  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.         txtInfo = (TextView) findViewById(R.id.txtInfo);  
  25.         Button btnIsAirPlane = (Button) findViewById(R.id.btnIsAirPlane);  
  26.         btnIsAirPlane.setOnClickListener(new Button.OnClickListener() {  
  27.             public void onClick(View view) {  
  28.                 isAirplaneModeOn();  
  29.             }  
  30.         });  
  31.         final Button btnSetAirPlane = (Button) findViewById(R.id.btnSetAirPlane);  
  32.         btnSetAirPlane.setOnClickListener(new Button.OnClickListener() {  
  33.             public void onClick(View view) {  
  34.                 setAirplaneMode(true);  
  35.             }  
  36.         });  
  37.         final Button btnCancelAirPlane = (Button) findViewById(R.id.btnCancelAirPlane);  
  38.         btnCancelAirPlane.setOnClickListener(new Button.OnClickListener() {  
  39.             public void onClick(View view) {  
  40.                 setAirplaneMode(false);  
  41.             }  
  42.         });  
  43.         IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");  
  44.         BroadcastReceiver receiver = new BroadcastReceiver() {  
  45.             @Override  
  46.             public void onReceive(Context context, Intent intent) {  
  47.                 Log.d("ANDROID_INFO""Service state changed");  
  48.                 Bundle bundle = intent.getExtras();  
  49.                 if (bundle != null) {  
  50.                     txtInfo.append(formateToLogTime(System.currentTimeMillis()) + " Service state changed action="  
  51.                             + intent.getAction() + "/n");  
  52.                     /** 
  53.                      * Set<String> set = bundle.keySet();<br/> 
  54.                      * key=manual value=0 <br/> 
  55.                      * key=cdmaRoamingIndicator value=-1 Integer<br/> 
  56.                      * key=operator-numeric value=0<br/> 
  57.                      * key=cssIndicator value=0 Boolean<br/> 
  58.                      * key=operator-alpha-long value=0<br/> 
  59.                      * key=networkId value=-1<br/> 
  60.                      * key=state value=0 Integer<br/> 
  61.                      * key=emergencyOnly value=0 Boolean<br/> 
  62.                      * key=systemId value=-1 Integer<br/> 
  63.                      * key=roaming value=0 Boolean<br/> 
  64.                      * key=operator-alpha-short value=0<br/> 
  65.                      * key=cdmaDefaultRoamingIndicator value=-1 Integer<br/> 
  66.                      * key=radioTechnology value=2 Integer<br/> 
  67.                      */  
  68.                     int state = bundle.getInt("state");  
  69.                     Log.d("ANDROID_INFO""state = " + state);  
  70.                     txtInfo.append(formateToLogTime(System.currentTimeMillis()) + " state = " + state);  
  71.                     switch (state) {  
  72.                     case 0x00:  
  73.                         Log.d("ANDROID_INFO""Connect the net successfully.");  
  74.                         txtInfo.append(" Connect the net successfully.");  
  75.                         btnSetAirPlane.setEnabled(true);  
  76.                         btnCancelAirPlane.setEnabled(false);  
  77.                         break;  
  78.                     case 0x01:  
  79.                         Log.d("ANDROID_INFO""Try to connect the net.");  
  80.                         txtInfo.append(" Try to connect the net.");  
  81.                         btnSetAirPlane.setEnabled(false);  
  82.                         btnCancelAirPlane.setEnabled(true);  
  83.                         break;  
  84.                     case 0x03:  
  85.                         Log.d("ANDROID_INFO""Set AirPlaneMode Successful.");  
  86.                         txtInfo.append(" Set AirPlaneMode Successful.");  
  87.                         btnSetAirPlane.setEnabled(false);  
  88.                         btnCancelAirPlane.setEnabled(true);  
  89.                         break;  
  90.                     }  
  91.                     txtInfo.append("/n");  
  92.                 } else {  
  93.                     Log.d("ANDROID_INFO""bundle is null");  
  94.                 }  
  95.             }  
  96.         };  
  97.         registerReceiver(receiver, intentFilter);  
  98.     }  
  99.     private void isAirplaneModeOn() {  
  100.         // 返回值是1时表示处于飞行模式  
  101.         int modeIdx = Settings.System.getInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);  
  102.         boolean isEnabled = (modeIdx == 1);  
  103.         txtInfo.append(formateToLogTime(System.currentTimeMillis()) + " AirPlaneMode " + isEnabled + "/n");  
  104.     }  
  105.     private void setAirplaneMode(boolean setAirPlane) {  
  106.         Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, setAirPlane ? 1 : 0);  
  107.         // 广播飞行模式信号的改变,让相应的程序可以处理。  
  108.         // 不发送广播时,在非飞行模式下,Android 2.2.1上测试关闭了Wifi,不关闭正常的通话网络(如GMS/GPRS等)。  
  109.         // 不发送广播时,在飞行模式下,Android 2.2.1上测试无法关闭飞行模式。  
  110.         Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);  
  111.         // intent.putExtra("Sponsor", "Sodino");  
  112.         // 2.3及以后,需设置此状态,否则会一直处于与运营商断连的情况  
  113.         intent.putExtra("state", setAirplane);  
  114.         sendBroadcast(intent);  
  115.         Toast toast = Toast.makeText(this"飞行模式启动与关闭需要一定的时间,请耐心等待", Toast.LENGTH_LONG);  
  116.         toast.show();  
  117.     }  
  118.     /** 
  119.      * 将长整型时间数字转为字符串。 
  120.      *  
  121.      * @return 返回格式为: 22:15:09的时间 
  122.      */  
  123.     public static String formateToLogTime(long time) {  
  124.         Calendar calendar = Calendar.getInstance();  
  125.         calendar.setTimeInMillis(time);  
  126.         SimpleDateFormat simpleDF = new SimpleDateFormat("HH:mm:ss");  
  127.         String logTime = simpleDF.format(calendar.getTime());  
  128.         return logTime;  
  129.     }  
  130. }  

补充下,得加个权限:

[xhtml] view plain copy
  1. <uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>  

0 0