广播接收器不适用于语音网络

时间:2011-06-17 03:23:48

标签: android broadcastreceiver

我想在移动网络连接丢失或收到时收到通知。通过以下代码,我可以收到有关Wi-Fi(数据)连接的通知,但不会收到移动(语音)连接的通知。

清单:

<uses-permission android:name="android.permission.ACCESS_NETWORK_ST ATE"/>

<application android:icon="@drawable/icon" android:label="@string/app_name">
 <receiver android:name=".notifier">
     <intent-filter>
         <action android:name="android.net.conn.CONNECTIVITY_CHANGE " />
     </intent-filter>
 </receiver>
</application>

Java:

public class notifier extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast toast = Toast.makeText(context, "Network Changed !!", Toast.LENGTH_LONG);
        toast.show();
    }
}

请告诉我,如何收到移动网络(语音)的通知。

2 个答案:

答案 0 :(得分:1)

伪代码!!

PhoneStateListener myListener = new PhoneStateListener() {
    @Override
    public void onServiceStateChanged (ServiceState serviceState) {
        // Handle different service states here.
    }
};

((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE))
    .listen(myListener, PhoneStateListener.LISTEN_SERVICE_STATE);

http://developer.android.com/reference/android/telephony/TelephonyManager.html#listen(android.telephony.PhoneStateListener, int)

答案 1 :(得分:0)

首先检查AndroidManifest.xml中的权限。

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>

并尝试以下代码。

ConnectivityManager manager = (ConnectivityManager)appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(mobile.isConnected()) {
    //Do Sth.
}

如果这不起作用,请检查手机的品牌。如果手机的开发人员阻止了某些代码或者没有实现某些代码,则它无效。你的手机是什么?