Broadcast Receiver not working for voice network

两盒软妹~` 提交于 2020-02-08 05:18:24

问题


I want to receive notification when Mobile network connection is lost or received. Through following code, I can receive notification for Wi-Fi (Data) connection but not for Mobile (Voice) connection.

Manifest :

<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();
    }
}

Please tell me, how can I receive notification for Mobile Network (Voice).


回答1:


Pseudocode!!

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)




回答2:


Check the permission in the AndroidManifest.xml, first.

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

And try this code below.

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

If this is not work, check your phone's brand. If the phone's developer blocked some codes or not implement some code, it doesn't work. What is your phone?



来源:https://stackoverflow.com/questions/6381010/broadcast-receiver-not-working-for-voice-network

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!