当我在android中有传入和传出数据时,我使用以下代码接收广播,但是当我关闭并打开wifi以测试它时,我没有收到广播。
Android清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.neo.testtrack1" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".IncomingCall"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
代码: -
public class IncomingCall extends BroadcastReceiver {
Context c ;
public void onReceive(Context context, Intent intent) {
c = context;
try {
// TELEPHONY MANAGER class object to register one listner
TelephonyManager tmgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
//Create Listner
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
// Register listener for LISTEN_CALL_STATE
tmgr.listen(PhoneListener,PhoneStateListener.LISTEN_DATA_ACTIVITY | PhoneStateListener.LISTEN_CALL_STATE | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
} catch (Exception e) {
Log.e("Phone Receive Error", " " + e);
}
}
private class MyPhoneStateListener extends PhoneStateListener {
@Override
public void onDataActivity(int direction) {
super.onDataActivity(direction);
Toast.makeText(c,"Data activity",Toast.LENGTH_SHORT).show();
String LOG_TAG = "Connection state";
switch (direction) {
case TelephonyManager.DATA_ACTIVITY_NONE:
Log.d(LOG_TAG, "onDataActivity: DATA_ACTIVITY_NONE");
break;
case TelephonyManager.DATA_ACTIVITY_IN:
Log.d(LOG_TAG, "onDataActivity: DATA_ACTIVITY_IN");
break;
case TelephonyManager.DATA_ACTIVITY_OUT:
Log.d(LOG_TAG, "onDataActivity: DATA_ACTIVITY_OUT");
break;
case TelephonyManager.DATA_ACTIVITY_INOUT:
Log.d(LOG_TAG, "onDataActivity: DATA_ACTIVITY_INOUT");
Toast.makeText(c,"Data connection inout",Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.DATA_ACTIVITY_DORMANT:
Log.d(LOG_TAG, "onDataActivity: DATA_ACTIVITY_DORMANT");
break;
default:
Log.d(LOG_TAG, "onDataActivity: UNKNOWN " + direction);
break;
}
}
}
}