如何在应用程序未连接到Internet时启动“设置”应用程序

时间:2014-01-30 09:46:29

标签: android android-intent

我正在构建一个使用权限的应用程序

android.permission.Internet

因为我想从mysql数据库启动数据并填充列表视图。当我连接到互联网时,应用程序启动正常,但当我不是它给我一个错误,并说应用程序已停止工作。如果我没有连接到wifi或数据来向用户发出警报,这将使他有机会打开设置应用程序并打开wifi或数据,这怎么可能?

3 个答案:

答案 0 :(得分:1)

查看是否有连接:

ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean network_connected = activeNetwork != null
                && activeNetwork.isConnectedOrConnecting();

getActiveNetworkInfo是您可以使用的最佳功能,因为它已经检查是否有任何连接,您不必关心wifi或数据或任何细节,因为只需提供当前活动连接,如果没有,则为null,当你只需要知道是否存在任何连接时,这是最好的。

您可以阅读here

  

返回有关当前活动的默认数据网络的详细信息。什么时候   已连接,此网络是传出连接的默认路由。   在启动网络之前,您应该始终检查isConnected()   交通。当没有默认网络时,这可能会返回null。

您需要获得许可

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

如果未连接,您可以通过以下方式打开设备的无线设置页面:

context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));

用于显示对话框:

AlertDialog.Builder bld = new AlertDialog.Builder(c);
bld.setMessage("The text you want to show");
    bld.setTitle(tit);
    bld.setPositiveButton(positiveButton, positiveOrNeutralCallback);

 bld.setNegativeButton("text for cancel button",new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

      bld.setPositiveButton("text for open settings button",new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                 context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
            }
        });


}
 bld.create().show();

<强>更新

您的代码将是:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  listView = (ListView) findViewById(R.id.listView1);
  accessWebService();
  registerCallClickBack();


 ConnectivityManager cm = (ConnectivityManager) context
         .getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean network_connected = activeNetwork != null
         && activeNetwork.isConnectedOrConnecting();


if (!network_connected)
  onDetectNetworkState().show();
 }


public AlertDialog onDetectNetworkState(){
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setMessage("text for message")
.setTitle("text for title")
.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        MainActivity.this.finish();
    }
})       
.setPositiveButton("Οκ",new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
    }
});
    return builder1.create();
} 

答案 1 :(得分:0)

您可以使用

检查互联网连接
  public static boolean checkNetworkConnection(Context context)
{
    final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    final android.net.NetworkInfo wifi =connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    final android.net.NetworkInfo mobile =connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if(wifi.isAvailable()||mobile.isAvailable())
        return true;
    else
        return false;
}

要启动设置,请使用

startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);

答案 2 :(得分:0)

结合Carlos的回答,您应该检查是否已连接到服务器。 你可以这样做:

ConnectivityManager cm = null;
NetworkInfo netInfo    = null;

cm = (ConnectivityManager)myActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
netInfo = cm.getActiveNetworkInfo();

if (netInfo != null && netInfo.isConnected()) {
  //we are connected to a network but can we connect to our server?
  //let's ping it
  Socket socket = null;
  try {

   socket = new Socket();
   if (socket != null) {

   socket.connect(new InetSocketAddress("yourServer.com", 80),   5000);                                 
   // we have reached our server --> lets get the data we want                              

   } catch (UnknownHostException e) {
    //Connectivity YES, Connection to server NO --> no data load
   } catch (IOException e) {
    //Connectivity YES, Connection to server NO --> no data load
   }
}