本文介绍了android.net.wifi.WIFI_STATE_CHANGED没有广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我需要重新启动,如果一个网络更改连接的服务。目前,它只能单向(无线移动数据),但它不工作的其他方式(移动数据到无线网络。)这是为什么?是不是因为我没有收到 android.net.wifi.WIFI_STATE_CHANGED 在我的广播接收器或者放错地方的权限?

In my app, I need a service restarted if a network change is connected. Currently it only works one way (wifi to mobile data) but it doesn't work the other way (mobile data to wifi.) Why is this? Is it because I'm not getting android.net.wifi.WIFI_STATE_CHANGED in my broadcast receiver or maybe a misplaced permission?

感谢您的帮助。

code:接收器清单条目:

Code:Manifest entry for receiver:

<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>

该接收机本身:

The receiver itself:

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

相关权限,我所列举的:

Relevant permissions I have listed:

  • android.permission.ACCESS_WIFI_STATE
  • android.permission.ACCESS_NETWORK_STATE
  • android.permission.INTERNET对

再次谢谢!

推荐答案

android.net.wifi.WIFI_STATE_CHANGED 只有当无线网络启用或禁用发送。你需要赶上 android.net.wifi.STATE_CHANGE 如果你想,当你连接到或WiFi网络断开连接接收广播事件了。

android.net.wifi.WIFI_STATE_CHANGED is sent only when WiFi is enabled or disabled. You need to catch android.net.wifi.STATE_CHANGE too if you want to receive broadcast event when you are connected to or disconnected from a WiFi network.

这篇关于android.net.wifi.WIFI_STATE_CHANGED没有广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 16:54