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