当前位置:   article > 正文

通过广播检测apn切换

android检测apn是否切换
receiver是在mainfest文件内注册的。

mainfest 代码
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"	package="com.nico.apn" android:versionCode="1" android:versionName="1.0">	<uses-sdk android:minSdkVersion="8" />	<application android:icon="@drawable/icon" android:label="@string/app_name">		<activity android:name=".TestApn" android:label="@string/app_name">			<intent-filter>				<action android:name="android.intent.action.MAIN" />				<category android:name="android.intent.category.LAUNCHER" />			</intent-filter>		</activity>		<receiver android:name=".ApnReceiver">			<intent-filter>				<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />			</intent-filter>		</receiver>			</application>	<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission></manifest>


receiver 类

public class ApnReceiver extends BroadcastReceiver {	@Override	public void onReceive(Context context, Intent intent) {		if (intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE")) {			//apn状态切换.		}	}}


	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		ApnReceiver r = new ApnReceiver();		this.registerReceiver(r, new IntentFilter(				"android.net.conn.CONNECTIVITY_CHANGE"));	}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/988233
推荐阅读
相关标签
  

闽ICP备14008679号