通过广播检测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"));
	}

你可能感兴趣的:(广播)