我在Android中使用Service
时很新,我很困惑。我有Activity
并且我想检查网络连接是否已启动(wifi或3g)。我实施了一个简单的Service
和一个BroadcastReveiver
。
这里是Service
public class NetworkCheck extends IntentService {
private final String CONNECTION = "connection";
public NetworkCheck(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent workIntent) {
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);
String dataString = workIntent.getDataString();
if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
// notify user you are not online
Intent localIntent = new Intent().putExtra(CONNECTION, 0);
LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
}
}
这里是BroadcastReceiver
public class NetworkReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
int value = intent.getIntExtra("connection", 0);
if(value == 0){
//now I print, then I will show up a dialog
System.out.println("NO CONNECTION!!!!!!!!");
}
}
}
这是Manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
这里有我在onCreate
Activity
方法中添加的代码
this.intent = new Intent(this, NetworkCheck.class);
this.startService(this.intent);
LocalBroadcastManager.getInstance(this).registerReceiver(new NetworkReceiver(), new IntentFilter());
这不起作用,我无法弄清楚原因。
答案 0 :(得分:5)
建议不要一直检查连接,只在需要时才打电话:
<强>无线网络强>:
public boolean isConnectedWifi(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return networkInfo.isConnected();
}
<强> 3G 强>:
public boolean isConnected3G(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
return networkInfo.isConnected();
}
答案 1 :(得分:1)
您在活动中注册的IntentFilter
没有任何与之关联的过滤器。
由于您使用&#34; connection&#34;的操作创建了一个意图。 (你的CONNECTION
常量),你需要创建一个IntentFilter,并使用相同的动作来触发BroadcastReceiver。
例如:
LocalBroadcastManager.getInstance(this).registerReceiver(
new NetworkReceiver(), new IntentFilter("connection"));
答案 2 :(得分:1)
您是否添加了以下广播接收器的意图过滤器。
<receiver android:name=".BroadcastReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
这用于检查网络连接。
public boolean isOnline(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in air plan mode it will be null
return (netInfo != null && netInfo.isConnected());
}
答案 3 :(得分:0)
您的测试显示当前网络状态不正确。如果WIFI或移动数据不可用,此测试将返回false。
if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED)
如果您想查看网络数据是否可用,请查看official documentation handles the test的方式。您可以用以下内容替换您的测试:
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();