android打开关闭手机飞行模式吗,Android开启/关闭/监听 飞行模式

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 set = bundle.keySet();

58. * key=manual value=0

59. * key=cdmaRoamingIndicator value=-1 Integer

60. * key=operator-numeric value=0

61. * key=cssIndicator value=0 Boolean

62. * key=operator-alpha-long value=0

63. * key=networkId value=-1

64. * key=state value=0 Integer

65. * key=emergencyOnly value=0 Boolean

66. * key=systemId value=-1 Integer

67. * key=roaming value=0 Boolean

68. * key=operator-alpha-short value=0

69. * key=cdmaDefaultRoamingIndicator value=-1 Integer

70. * key=radioTechnology value=2 Integer

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值