android 关闭服务代码,Android通过代码打开和关闭网络连接

我们都知道,要打开网络连接是要有相应的权限的,但是目前Android中并没有直接打开和关闭网络连接的权限,因此我们只能通过APN来实现:

由于Android对于APN的网络API没有公开,不过我们可以阅读源代码,然后进行数据库操作,系统会自动监听数据库的变化,从而实现开启或者关闭APN。

其实原理很简单 :

1 、 当开启APN的时候,设置一个正确的移动或者联通的APN

2、 关闭的时候设置一个错误APN就会自动关闭网络

下面是源代码:

package com.mandr.network.demo;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;

import android.content.ContentValues;

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

public class OpenNetworkActivity extends Activity {

private Button open, close;

Uri uri =

Uri.parse("content://telephony/carriers");

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

open = (Button)

findViewById(R.id.open);

close = (Button)

findViewById(R.id.close);

open.setOnClickListener(new

View.OnClickListener() {

public

void onClick(View v) {

openAPN();

}

});

close.setOnClickListener(new

View.OnClickListener() {

public

void onClick(View v) {

closeAPN();

}

});

}

public void openAPN() {

List list = getAPNList();

for (APN apn : list) {

ContentValues

cv = new ContentValues();

cv.put("apn",

APNMatchTools.matchAPN(apn.apn));

cv.put("type",

APNMatchTools.matchAPN(apn.type));

getContentResolver().update(uri,

cv, "_id=?",

new

String[] { apn.id });

}

}

public void closeAPN() {

List list = getAPNList();

for (APN apn : list) {

ContentValues

cv = new ContentValues();

cv.put("apn",

APNMatchTools.matchAPN(apn.apn) + "mdev");

cv.put("type",

APNMatchTools.matchAPN(apn.type) + "mdev");

getContentResolver().update(uri,

cv, "_id=?",

new

String[] { apn.id });

}

}

private List getAPNList() {

String tag =

"Main.getAPNList()";

// current不为空表示可以使用的APN

String projection[] = {

"_id,apn,type,current" };

//查询数据库

Cursor cr =

this.getContentResolver().query(uri, projection, null,

null,

null);

List list = new

ArrayList();

while (cr != null &&

cr.moveToNext()) {

Log.d(tag,

cr.getString(cr.getColumnIndex("_id"))

+ " "

+

cr.getString(cr.getColumnIndex("apn")) + " "

+

cr.getString(cr.getColumnIndex("type")) + " "

+

cr.getString(cr.getColumnIndex("current")));

APN a = new

APN();

a.id =

cr.getString(cr.getColumnIndex("_id"));

a.apn =

cr.getString(cr.getColumnIndex("apn"));

a.type =

cr.getString(cr.getColumnIndex("type"));

list.add(a);

}

if (cr != null)

cr.close();

return list;

}

public static class APN {

String id;

String apn;

String type;

}

}

APNMatchTools.java

package com.mandr.network.demo;

public class APNMatchTools {

public static class APNNet {

public static String CMWAP =

"cmwap";

public static String CMNET =

"cmnet";

// 中国联通3GWAP设置 中国联通3G因特网设置

中国联通WAP设置 中国联通因特网设置

// 3gwap 3gnet uniwap

uninet

public static String GWAP_3

= "3gwap";

public static String GNET_3

= "3gnet";

public static String UNIWAP

= "uniwap";

public static String UNINET

= "uninet";

}

public static String matchAPN(String

currentName) {

if ("".equals(currentName) ||

null == currentName) {

return

"";

}

currentName =

currentName.toLowerCase();

if

(currentName.startsWith(APNNet.CMNET))

return

APNNet.CMNET;

else if

(currentName.startsWith(APNNet.CMWAP))

return

APNNet.CMWAP;

else if

(currentName.startsWith(APNNet.GNET_3))

return

APNNet.GNET_3;

else if

(currentName.startsWith(APNNet.GWAP_3))

return

APNNet.GWAP_3;

else if

(currentName.startsWith(APNNet.UNINET))

return

APNNet.UNINET;

else if

(currentName.startsWith(APNNet.UNIWAP))

return

APNNet.UNIWAP;

else if

(currentName.startsWith("default"))

return

"default";

else

return

"";

// return

currentName.substring(0, currentName.length() -

// SUFFIX.length());

}

}

最后别忘了加入修改APN的权限:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值