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" />

更多相关文章

  1. Android实现开机自启动Service
  2. Android(安卓)获取设备序列号(SN号)含源码Demo
  3. android 发送短信的两种方式
  4. Android电源管理简介(下)
  5. android版本兼容问题总结
  6. android 状态栏移动到底部
  7. android UI之去掉状态栏
  8. android滑动改变状态栏颜色及透明度
  9. Android完全退出应用程序的方法

随机推荐

  1. 关于android receiver
  2. Android(安卓)permission第三方rxpermiss
  3. Android(安卓)Preferencescreen用法
  4. Android 解屏幕锁与点亮屏幕
  5. Android启动页的全屏处理方案
  6. Android在Context详细解释 ---- 你不知道
  7. [置顶] Android(安卓)菜单:OptionMenu 介
  8. android studio生成jar包
  9. android中Acticity之间的跳转
  10. Android 定时重启Android系统源(手机root