簡體   English   中英

如何設置禁用移動數據的接收器?

[英]how to set receiver for mobile data enabled disabled?

我想將接收器設置為啟用/禁用的任何類型的移動數據,例如3g等。

我正在使用此代碼接收...

 ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
        NetworkInfo netInfo = conMan.getActiveNetworkInfo();

        if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_MOBILE) {


               Toast.makeText(context, "Mobile data enable", Toast.LENGTH_LONG).show();


             }

這個表情很明顯

<receiver android:name=".BroadcastReceiverforWifiBluetoothData" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" /> </intent-filter> </receiver>

但是在某些設備中它無法正常工作,很多情況下,接收器會在一段時間后觸發,一段時間后我不會觸發Broadcastreceiver的onReceive()嗎?

請幫我解決錯誤的代碼,還是需要其他技巧來解決?

提前致謝...

試試這個代碼,

boolean enabled=prefs.getBoolean(key, false);
    int flag=(enabled ?
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
    ComponentName component=new ComponentName(EditPreferences.this, OnBootReceiver.class);

    getPackageManager()
        .setComponentEnabledSetting(component, flag,
                                    PackageManager.DONT_KILL_APP);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM