的是Android提供任何监听器或回调方法连接或断开到网络的时候? [英] Is Android providing any listeners or callback methods when connected or disconnected to a network?

查看:130
本文介绍了的是Android提供任何监听器或回调方法连接或断开到网络的时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

反正是有得到通知或predefined回电连接时或断开到WiFi网络。为了更清楚,Intially装置被连接到一个无线网络,并且开始上传一组图像。试想一下,上传完成之前,网络连接将被终止。现在我的问题是我怎么能检查是否重新建立的网络连接?是Android正在提供任何监听器或回调方式通知该设备再次连接到网络(任何网络WiFi或3G).Could任何人都可以指导我一个很好的solution.I需要是很糟糕。

     

在此先感谢

解决方案
  

在你的接收机使用该

 <接收机器人:名称=。UpdateReceiver>
    <意向滤光器>
        <作用机器人:名称=android.net.conn.CONNECTIVITY_CHANGE/>
    &所述; /意图滤光器>
< /接收器
 

  

您UpdateRecieiver

 公共类UpdateReceiver扩展的BroadcastReceiver {

  @覆盖
  公共无效的onReceive(上下文的背景下,意图意图){

  ConnectivityManager connectivityManager =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
  的NetworkInfo activeNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
  布尔isConnected = activeNetInfo = NULL和放大器;!&安培; activeNetInfo.isConnectedOrConnecting();
  如果(isConnected)
      Log.i(NET,connecte+ isConnected);
  其他Log.i(NET,不connecte+ isConnected);
  }
}
 

<一个href="http://stackoverflow.com/questions/10545822/how-to-make-call-to-service-from-receiver-depends-on-internet-connected-or-not/10546152#10546152">for更多信息请参阅本

Is there anyway to get notification or a predefined call back when connected or disconnect to a wifi network. To be more clear, Intially device is connected to a wifi network, and started uploading a set of images. Imagine,before uploading is completed, network connection is terminated. Now my problem is how can I check whether network connection is re-established? Is Android is providing any listeners or call back methods to notify that device is again connected to a network(any network wifi or 3g).Could anyone can guide me to a good solution.I need it badly.

Thanks in Advance

解决方案

Use this in your receiver

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

Your UpdateRecieiver

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

for more info refer this

这篇关于的是Android提供任何监听器或回调方法连接或断开到网络的时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆