android GPRS
package com.example;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
public class GprsEnable extends Activity {
public final static int LOGIN_DIALOG = 1;
private ConnectivityManager mConnectManager;
/** The open gprs time counter we remember. */
private int num = 0;
private ProgressDialog mDialog;
/** Open the gprs. */
public void setNetWorkEnable(String cmd) {
String[] args = new String[3];
args[0] = "svc";
args[1] = "data";
args[2] = cmd;
try {
Process process = Runtime.getRuntime().exec(args);
// get the err line
InputStream stderr = process.getErrorStream();
InputStreamReader isrerr = new InputStreamReader(stderr);
BufferedReader brerr = new BufferedReader(isrerr);
// get the output line
InputStream outs = process.getInputStream();
InputStreamReader isrout = new InputStreamReader(outs);
BufferedReader brout = new BufferedReader(isrout);
String line = null;
String result = "";
// get the whole error message string
while ((line = brerr.readLine()) != null) {
result += line;
result += "\n";
}
if (result != "") {
// put the result string on the screen
System.out.println("the error outcome is ___" + result);
}
result = "";
// get the whole standard output string
while ((line = brout.readLine()) != null) {
result += line;
result += "\n";
}
if (result != "") {
// put the result string on the screen
System.out.println("the outcome is ___" + result);
}
if (!cmd.equalsIgnoreCase("disable")) {
try {
Thread.sleep(2000);
checkState();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (Throwable t) {
t.printStackTrace();
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showDialog(GprsEnable.LOGIN_DIALOG);
mConnectManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (checkWifiStatus()) {
dismissDialog(GprsEnable.LOGIN_DIALOG);
return;
}
if (!checkGprsStatus()) {
setNetWorkEnable("enable");
return;
}
dismissDialog(GprsEnable.LOGIN_DIALOG);
}
/** Check the wifi is open or not. */
public boolean checkWifiStatus() {
return mConnectManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState() == NetworkInfo.State.CONNECTED ? true : false;
}
/** Check the Gprs is open or not. */
public boolean checkGprsStatus() {
return mConnectManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.getState() == NetworkInfo.State.CONNECTED ? true : false;
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case GprsEnable.LOGIN_DIALOG:
mDialog = new ProgressDialog(GprsEnable.this);
mDialog.setMessage("GPRS开启中....");
return mDialog;
default:
return null;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case 4:
if (checkGprsStatus()) {
setNetWorkEnable("disable");
}
finish();
break;
}
return true;
}
/** Check the gprs is opened or not,if not try to open one time again. */
public void checkState() {
num++;
if (!checkGprsStatus() && num < 2) {
setNetWorkEnable("enable");
} else {
dismissDialog(GprsEnable.LOGIN_DIALOG);
}
}
}
需要添加的权限:Java代码
<!-- 查询网络状态权限 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 修改手机连接网路状态权限 -->
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
![](http://www.cnblogs.com/images/cnblogs_com/xsmhero/20091027.jpg)