[Android]开启/关闭/监听 飞行模式

不多说,上图,见代码。

 

[Android]开启/关闭/监听 飞行模式_第1张图片

 


 

package lab.sodino.airplane;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
 * @author Sodino Email:[email protected]
 * */
public class AirPlaneAct extends Activity {
	private TextView txtInfo;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		txtInfo = (TextView) findViewById(R.id.txtInfo);
		Button btnIsAirPlane = (Button) findViewById(R.id.btnIsAirPlane);
		btnIsAirPlane.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View view) {
				isAirplaneModeOn();
			}
		});
		final Button btnSetAirPlane = (Button) findViewById(R.id.btnSetAirPlane);
		btnSetAirPlane.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View view) {
				setAirplaneMode(true);
			}
		});
		final Button btnCancelAirPlane = (Button) findViewById(R.id.btnCancelAirPlane);
		btnCancelAirPlane.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View view) {
				setAirplaneMode(false);
			}
		});
		IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");
		BroadcastReceiver receiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				Log.d("ANDROID_INFO", "Service state changed");
				Bundle bundle = intent.getExtras();
				if (bundle != null) {
					txtInfo.append(formateToLogTime(System.currentTimeMillis()) + " Service state changed action="
							+ intent.getAction() + "/n");
					/**
					 * Set<String> set = bundle.keySet();<br/>
					 * key=manual value=0 <br/>
					 * key=cdmaRoamingIndicator value=-1 Integer<br/>
					 * key=operator-numeric value=0<br/>
					 * key=cssIndicator value=0 Boolean<br/>
					 * key=operator-alpha-long value=0<br/>
					 * key=networkId value=-1<br/>
					 * key=state value=0 Integer<br/>
					 * key=emergencyOnly value=0 Boolean<br/>
					 * key=systemId value=-1 Integer<br/>
					 * key=roaming value=0 Boolean<br/>
					 * key=operator-alpha-short value=0<br/>
					 * key=cdmaDefaultRoamingIndicator value=-1 Integer<br/>
					 * key=radioTechnology value=2 Integer<br/>
					 */
					int state = bundle.getInt("state");
					Log.d("ANDROID_INFO", "state = " + state);
					txtInfo.append(formateToLogTime(System.currentTimeMillis()) + " state = " + state);
					switch (state) {
					case 0x00:
						Log.d("ANDROID_INFO", "Connect the net successfully.");
						txtInfo.append(" Connect the net successfully.");
						btnSetAirPlane.setEnabled(true);
						btnCancelAirPlane.setEnabled(false);
						break;
					case 0x01:
						Log.d("ANDROID_INFO", "Try to connect the net.");
						txtInfo.append(" Try to connect the net.");
						btnSetAirPlane.setEnabled(false);
						btnCancelAirPlane.setEnabled(true);
						break;
					case 0x03:
						Log.d("ANDROID_INFO", "Set AirPlaneMode Successful.");
						txtInfo.append(" Set AirPlaneMode Successful.");
						btnSetAirPlane.setEnabled(false);
						btnCancelAirPlane.setEnabled(true);
						break;
					}
					txtInfo.append("/n");
				} else {
					Log.d("ANDROID_INFO", "bundle is null");
				}
			}
		};
		registerReceiver(receiver, intentFilter);
	}
	private void isAirplaneModeOn() {
		// 返回值是1时表示处于飞行模式
		int modeIdx = Settings.System.getInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
		boolean isEnabled = (modeIdx == 1);
		txtInfo.append(formateToLogTime(System.currentTimeMillis()) + " AirPlaneMode " + isEnabled + "/n");
	}
	private void setAirplaneMode(boolean setAirPlane) {
		Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, setAirPlane ? 1 : 0);
		// 广播飞行模式信号的改变,让相应的程序可以处理。
		// 不发送广播时,在非飞行模式下,Android 2.2.1上测试关闭了Wifi,不关闭正常的通话网络(如GMS/GPRS等)。
		// 不发送广播时,在飞行模式下,Android 2.2.1上测试无法关闭飞行模式。
		Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
		// intent.putExtra("Sponsor", "Sodino");
		// 2.3及以后,需设置此状态,否则会一直处于与运营商断连的情况
		intent.putExtra("state", setAirplane);
		sendBroadcast(intent);
		Toast toast = Toast.makeText(this, "飞行模式启动与关闭需要一定的时间,请耐心等待", Toast.LENGTH_LONG);
		toast.show();
	}
	/**
	 * 将长整型时间数字转为字符串。
	 * 
	 * @return 返回格式为: 22:15:09的时间
	 */
	public static String formateToLogTime(long time) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(time);
		SimpleDateFormat simpleDF = new SimpleDateFormat("HH:mm:ss");
		String logTime = simpleDF.format(calendar.getTime());
		return logTime;
	}
}

 

本文内容归CSDN博客博主Sodino所有

转载请注明出处:http://blog.csdn.net/sodino/archive/2011/01/13/6134196.aspx

 

补充下,得加个权限:

<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>

你可能感兴趣的:(android,String,service,calendar,null,button)