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

 

转载于:https://www.cnblogs.com/xsmhero/archive/2012/06/12/2545823.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值