簡體   English   中英

android.net.wifi.WIFI_STATE_CHANGED沒有播出

[英]android.net.wifi.WIFI_STATE_CHANGED not being broadcast

在我的應用程序中,如果連接了網絡更改,我需要重新啟動服務。 目前它只能以一種方式工作(wifi到移動數據),但它不能以其他方式工作(移動數據到wifi。)為什么會這樣? 是因為我沒有在我的廣播接收器中獲得android.net.wifi.WIFI_STATE_CHANGED或者可能是錯位的權限?

謝謝你的幫助。

代碼:接收器的清單條目:

<receiver
    android:name="LiveForever"
    android:label="NetworkChangeReceiver" >
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
    </intent-filter>
</receiver>

接收器本身:

public static final int TYPE_WIFI = 1;
public static final int TYPE_MOBILE = 2;
public static final int TYPE_NOT_CONNECTED = 0;
public static final String PREFS_NAME = "cakecloudpreferences";

@Override
public void onReceive(Context context, Intent intent) {
    SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
    if (getConnectivityStatus(context)!=0&&!settings.getBoolean("osmframerunning",false)) {
        context.stopService(new Intent(context, OSMFrame.class));
        settings.edit().putBoolean("osmframerunning",false).commit();
        Intent frameintent = new Intent(context,OSMFrame.class);
        frameintent.putExtra("username",settings.getString("usr",""));
        frameintent.putExtra("password",settings.getString("pswd",""));
        frameintent.putExtra("uid",settings.getString("uid",""));
        context.startService(frameintent);
        Log.i("CCLiveForever","LiveForever Triggered, OSMFrame restarted.");
    }
}

public int getConnectivityStatus(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (null != activeNetwork) {
        if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) return TYPE_WIFI;
        if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) return TYPE_MOBILE;
    } 
    return TYPE_NOT_CONNECTED;
}

我列出的相關權限:

  • android.permission.ACCESS_WIFI_STATE
  • android.permission.ACCESS_NETWORK_STATE
  • android.permission.INTERNET對

再次謝謝你!

僅當啟用或禁用WiFi時才會發送android.net.wifi.WIFI_STATE_CHANGED 如果您希望在連接到WiFi網絡或從WiFi網絡斷開連接時接收廣播事件,則還需要捕獲android.net.wifi.STATE_CHANGE

暫無
暫無

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

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