不多說,上圖,見代碼。
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;
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");
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();
}
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
補充下,得加個權限: