如何在Android中使用后台线程onDestroy监视活动中的网络状态?

时间:2012-06-23 09:44:52

标签: android

Hai我是Android的新手。我需要检查整个应用程序中的网络连接,并在状态发生变化时通知用户网络状态。请告诉我如何处理一些示例代码。提前知道

1 个答案:

答案 0 :(得分:0)

您可以使用runOnUiThreadConnectivityManager在固定时间后检查网络状态,并向用户显示消息:

private boolean mNetworkRunning=false;
private  ConnectivityManager connMgr; 
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView (R.layout.main);
 mClockRunning=true;
 NetworkStatusThread();
 }
   @Override
    protected void onDestroy() {
        super.onDestroy();
        // Stop Thread Here.
        mNetworkRunning=false;
    }  
 public void NetworkStatusThread(){
     Thread th=new Thread(){
    @Override
    public void run(){
     try
      {
       while(mNetworkRunning)
       {
         Thread.sleep(1000L);// set time here for refresh time in show update to user
        NetworkActivity.this.runOnUiThread(new Runnable() { 
         @Override
         public void run() {
         // TODO Auto-generated method stub
          if(mNetworkRunning)
           {
             boolean statu=isMobileNetworkAvailable();
             if(statu==true)
            Toast.makeText(YourCurrentActivity.this, "Network Available",Toast.LENGTH_LONG).show();
             else
             Toast.makeText(YourCurrentActivity.this, "Network Not Available",Toast.LENGTH_LONG).show();
           } 
          };
    }
    }catch (InterruptedException e) {
        // TODO: handle exception
    }
   }
 };
 th.start();
}

public  boolean isMobileNetworkAvailable(Context con){  
    if(null == connMgr){  
     connMgr = (ConnectivityManager)con.getSystemService(Context.CONNECTIVITY_SERVICE);  
    }  
    NetworkInfo wifiInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);  
    NetworkInfo mobileInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  
    if(wifiInfo.isAvailable()){  
        return true;  
    }else if(mobileInfo.isAvailable()){  
        return true;  
    }else{  
        return false;  
    }  
  }

和第二个选项没有使用Thread然后你可以注册CONNECTIVITY_CHANGE Manifest.xml的接收器:

< !-- Needed to check when the network connection changes -->   
< uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>   
< receiver android:name="you_package_name.ConnectionChangeReceiver"   
  android:label="NetworkConnection">   
< intent-filter>   
      < action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>   
< /intent-filter>   
< /receiver>

代码:

public class ConnectionChangeReceiver extends BroadcastReceiver {  

    private static final String TAG =ConnectionChangeReceiver.class.getSimpleName();  

    @Override   
    public void onReceive(Context context, Intent intent) {   
    boolean success = false;   
    ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);    
    State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();   
    //WIFI
    if (State.CONNECTED == state) {  
    success = true;   
    }   
    //GPRS
    state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();  
    if (State.CONNECTED != state) {   
    success = true;   
    }   
    if (!success) {   
    Toast.makeText(context, "SHOW STATUS HERE", Toast.LENGTH_LONG).show();   
    }    
    }