Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何从接收器调用服务取决于Internet连接与否?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道如何在活动中检查互联网是否可用,我的要求是,实际上当设备处于脱机状态(无互联网)时,我在DB中存储了一些值,当涉及到在线时必须将DB值传递给服务器没有用户交互性.我怎样才能做到这一点.
我得到了这个例子 How to check the Internet Connection periodically in whole application?,但它只适用于一项活动,如何使其成为全球性的.

解决方法

在接收器中使用它

<receiver android:name=".updateReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

你的updateRecieiver@H_450_15@

public class updateReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context,Intent intent) {


      Connectivitymanager connectivitymanager = (Connectivitymanager) 
                                   context.getSystemservice(Context.CONNECTIVITY_serviCE );
      NetworkInfo activeNeTinfo = connectivitymanager.getNetworkInfo(Connectivitymanager.TYPE_MOBILE);
      Boolean isConnected = activeNeTinfo != null && activeNeTinfo.isConnectedOrConnecTing();   
      if (isConnected)       
          Log.i("NET","connecte" +isConnected);   
      else Log.i("NET","not connecte" +isConnected);
    }
}

大佬总结

以上是大佬教程为你收集整理的android – 如何从接收器调用服务取决于Internet连接与否?全部内容,希望文章能够帮你解决android – 如何从接收器调用服务取决于Internet连接与否?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。