2015-08-21 39 views
0

我正在做一个有登录活动的应用程序,当我进入这个活动时,我要求用户启用WIFI或移动数据,如果没有连接到我的服务器。我在对话框中给出3个选项,WIFI,MOBILE DATA和CANCEL。有没有办法等待连接?回调?听众?

public void checkConnectionDialog(Context context){ 

     if(dialogConnection != null && dialogConnection.isShowing()) return; 

     dialogConnection = new Dialog(context); 
     dialogConnection.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialogConnection.setCanceledOnTouchOutside(false); 
     dialogConnection.setCancelable(false); 

     dialogConnection.setContentView(R.layout.dialog_connection); 

     Button bt_data = (Button) dialogConnection.findViewById(R.id.bt_data); 
     Button bt_wifi = (Button) dialogConnection.findViewById(R.id.bt_wifi); 
     Button bt_cancel = (Button) dialogConnection.findViewById(R.id.bt_cancel); 


     bt_data.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)); 
       dialogConnection.dismiss(); 
      } 
     }); 

     bt_wifi.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); 
       dialogConnection.dismiss(); 
      } 
     }); 

     bt_cancel.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dialogConnection.dismiss(); 
       setResult(RESULT_CANCELED, null); 
       finish(); 
      } 
     }); 

     dialogConnection.show(); 
    } 

通过Intents,我将用户发送到WIFI或MOBILE DATA设置。如果用户启用wifi或数据并返回(按下后退按钮),则系统需要几秒钟才能连接到互联网。

如果用户在启用互联网后单击回来,当他返回到应用程序时,它仍然没有连接。

我想知道的是如何让应用程序等待连接继续。

APP(未连接) - >意图(WiFi或已启用数据) - > APP(等到连接显示视图)

感谢您的帮助。

+0

创建一个BroadcastReceiver,当您连接到互联网时它将触发一个事件:) – Kedarnath

回答

1

您可以注册一个BroadcastReceiver,它监听您活动中的网络更改。事情是这样的:

BroadcastReceiver connectionReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent != null && intent.getAction().equals(ConnectivityManager 
       .CONNECTIVITY_ACTION)) { 

      boolean isConnected = ConnectivityManagerCompat.getNetworkInfoFromBroadcast 
        ((ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE), 
          intent).isConnected(); 

      if (isConnected) { 
       onConnectionEstablished(); 
      } else { 
       onConnectionAbsent(); 
      } 
     } 
    } 
}; 

连接建立和缺席只是空洞的方法这确实是你想要的东西,如:

public void onConnectionEstablished() { 
     // do something 
} 

public void onConnectionAbsent() { 
     // do something 
} 

注册和注销接收器和onResume

@Override 
protected void onResume() { 
    super.onResume(); 
    IntentFilter filter = new IntentFilter(); 
    filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); 
    registerReceiver(connectionReceiver, filter); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    try { 
     unregisterReceiver(connectionReceiver); 
    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
    } 
} 
0

您可以使用以下清单意图接收连接更改。把它们作为意图过滤器放在reciver内。请注意,这只是从头开始,我现在不能测试它!

<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> 
     <action android:name="android.net.wifi.STATE_CHANGE"/> 

您此接收每次连接发生了变化的时候,但你必须做你的广播接收器的一些工作,以检查它是否连接与否:

例如,你可以在的onReceive听() :

@Override 
public void onReceive(Context context, final Intent intent) { 
    ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); 
    NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

    if (info.isConnected()) { 
     //then You know it is connected and can do some stuff 
    }else{ 

     //not connected..... 
    } 
} 

例如,你现在可以开始服务做一些事情,告知您的用户它是连接与否。

相关问题