如何到Android应用程序连接到互联网时自动发现网络 [英] How To Connect my android Application To Internet Automatically whenever network found

查看:214
本文介绍了如何到Android应用程序连接到互联网时自动发现网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的基于IE WIFI网络连接的Andr​​oid应用程序/移动Network.it时,我的手机连接到互联网,但是当互联网连接断开,它停止工作(obesely),它仍然停止我的手机再次连接到互联网后,工作的正常工作。

My android application based on network connection i.e WIFI/Mobile Network.it works fine when my mobile is connected to internet but when internet connection disconnected it stops working(obesely) and it still stop working after my mobile again connected to internet.

我要自动启动我的应用程序,只要互联网络发现

请帮我先生,
谢谢!

Please Help me Sir, Thank you !

推荐答案

您可以使用广播接收器,只要网络可用U可以开始你的application.First创建一个后台服务,并启动服务时,设备启动检查网络状态up.Now在这个服务注册一个广播接收器和监控网络state.If网络可U可以启动应用程序,如果不可用,你可以关闭it.Please参考如下code广播接收者。你满意这个答案,然后不要别忘了投票了。

you can check network state using broadcast receiver whenever network is available u can start your application.First create a background service and start your service when device boot up.Now in this service register a broadcast receiver and monitor network state.If network is available u can start your application and if unavailable you can close it.Please refer code given below for broadcast receiver.If you are satisfied with this answer then don't forget to vote up.

public class BroadCastSampleActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.registerReceiver(this.mConnReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }

    private BroadcastReceiver mConnReceiver = new BroadcastReceiver()
    {
        public void onReceive(Context context, Intent intent)
        {
            boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
            String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
            boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

            NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

            if(currentNetworkInfo.isConnected())
            {
                Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
            }
        }
    };
}

这篇关于如何到Android应用程序连接到互联网时自动发现网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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