Activity 启动广播接收器检查网络状态
Activity starting Broadcast Receiver to check network status
我有一个 activity,我正在其上初始化一个广播接收器。我希望这个广播接收器在收到 phone 的网络状态已更改的消息后调用我的 activity 中的函数。
不幸的是,广播接收器似乎从未收到此消息,因为从未调用过 onReceive 函数。
这里是接收者:
private BroadcastReceiver NetworkReceiver= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (!wifi.isAvailable() || !mobile.isAvailable()) {
Log.d("debug"," in the if");
Handler handler = new Handler();
handler.postDelayed(sendUpdatesToUI, 10);
Log.d("Network Available ", "Flag No 1");
}
}
};
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
update();
}
};
private void update() {
Log.d("debug", "got update");
}
我在 onCreate 函数中用这一行调用注册它:
registerReceiver(NetworkReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
这是我的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rtuya.secmere">
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".LoginActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBar" />
</application>
</manifest>
编辑
我已将此添加到我的清单中,但仍然没有结果:
<receiver
android:name="NetworkReceiver"
android:label="NetworkReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
广播接收器也需要在您的 manifest.xml
中声明
添加
<receiver android:name=".ReceiverName" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
您可能希望将接收器放在它自己的 class 而不是匿名进行。
你不是必须在清单中注册接收者吗?像这样:
<receiver
android:name="Receiver"
android:label="Receiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
我有一个 activity,我正在其上初始化一个广播接收器。我希望这个广播接收器在收到 phone 的网络状态已更改的消息后调用我的 activity 中的函数。
不幸的是,广播接收器似乎从未收到此消息,因为从未调用过 onReceive 函数。
这里是接收者:
private BroadcastReceiver NetworkReceiver= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (!wifi.isAvailable() || !mobile.isAvailable()) {
Log.d("debug"," in the if");
Handler handler = new Handler();
handler.postDelayed(sendUpdatesToUI, 10);
Log.d("Network Available ", "Flag No 1");
}
}
};
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
update();
}
};
private void update() {
Log.d("debug", "got update");
}
我在 onCreate 函数中用这一行调用注册它:
registerReceiver(NetworkReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
这是我的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rtuya.secmere">
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".LoginActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBar" />
</application>
</manifest>
编辑
我已将此添加到我的清单中,但仍然没有结果:
<receiver
android:name="NetworkReceiver"
android:label="NetworkReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
广播接收器也需要在您的 manifest.xml
添加
<receiver android:name=".ReceiverName" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
您可能希望将接收器放在它自己的 class 而不是匿名进行。
你不是必须在清单中注册接收者吗?像这样:
<receiver
android:name="Receiver"
android:label="Receiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>