我想知道如何使用 BroadcastReceiver 检查何时连接移动数据。
这是我到目前为止所拥有的:
private BroadcastReceiver MobileDataStateChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(TelephonyManager.EXTRA_STATE,
TelephonyManager.DATA_DISCONNECTED);
if (state == TelephonyManager.DATA_CONNECTED) {
mobileStatus.setText("Connected");
} else if (state == TelephonyManager.DATA_DISCONNECTED) {
mobileStatus.setText("Disconnected");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.status_page);
mobileStatus = (TextView) this.findViewById(R.id.textView4);
registerReceiver(MobileDataStateChangedReceiver, new IntentFilter(
TelephonyManager.ACTION_PHONE_STATE_CHANGED));
}
我在这里做错了什么?
我在 Wi-Fi 检查中使用了相同的概念并且效果很好?我正在使用以下权限:
<uses-permission android:name="android.permission.INTERNET"/>