使用android上的服务检查internet连接
当我的应用程序使用“活动”打开时,我知道如何检查互联网连接。但当我的应用程序未运行时,如何检查服务中的连接?使用下面的代码,您可以测试设备是否连接到internet。 当您尝试调用任何Web服务或任何与internet相关的任务时,可以随时使用此功能。 您可以在后台运行的android服务中使用此功能使用android上的服务检查internet连接,android,service,Android,Service,当我的应用程序使用“活动”打开时,我知道如何检查互联网连接。但当我的应用程序未运行时,如何检查服务中的连接?使用下面的代码,您可以测试设备是否连接到internet。 当您尝试调用任何Web服务或任何与internet相关的任务时,可以随时使用此功能。 您可以在后台运行的android服务中使用此功能 public boolean isConnectingToInternet(Context _context){ ConnectivityManager connectivity
public boolean isConnectingToInternet(Context _context){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
public boolean连接到互联网(Context\u Context){
ConnectivityManager connectivity=(ConnectivityManager)_context.getSystemService(context.connectivity_SERVICE);
if(连接性!=null)
{
NetworkInfo[]info=connectivity.getAllNetworkInfo();
如果(信息!=null)
对于(int i=0;i
当网络连接发生变化时,系统会提供广播
,您可以使用。无论您的应用程序处于打开还是关闭状态,都将调用此命令。您可能需要使用广播接收器。您将在连接中连续接收更新。(已连接/已断开)
例如:
清单:
public class ConnectivityChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that which service class will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
YourService.class.getName());
intent.putExtra("isNetworkConnected",isConnected(context));
startService(context, (intent.setComponent(comp)));
}
public boolean isConnected(Context context) {
ConnectivityManager connectivityManager = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}
}
class YourService extends IntentService{
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
boolean isNetworkConnected = extras.getBoolean("isNetworkConnected");
// your code
}
}
权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
您的服务类别:
public class ConnectivityChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that which service class will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
YourService.class.getName());
intent.putExtra("isNetworkConnected",isConnected(context));
startService(context, (intent.setComponent(comp)));
}
public boolean isConnected(Context context) {
ConnectivityManager connectivityManager = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}
}
class YourService extends IntentService{
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
boolean isNetworkConnected = extras.getBoolean("isNetworkConnected");
// your code
}
}
当使用广播接收器时,为什么要使用套接字,在清单中注册它,并将此广播接收器的onReceive方法中更改的网络状态上的数据传递给您的服务。这并不能回答问题。OP说他知道怎么做只是当应用程序没有打开时这不是我的答案!!!我更新了答案:“我知道当我的应用程序打开时,如何在活动中检查互联网是否可用。”。您可以在android服务中使用此功能,您需要创建服务并启动它。您可能需要处理广播接收器来处理设备重启后的服务自动启动等。Tnx,因此,如果我想做一些事情(检查更新),我该怎么做?在广播接收器的onReceive方法中,您可以运行任意代码,因此,如果您想在后台进行一些处理,您可以启动一个或一些操作来完成setResultCode(Activity.RESULT\u OK);这是不正确的。为什么?您可以安全地删除该行。若应用程序未运行且待启动服务为后台服务,该行是否有效?