Android:在Application类中强制关闭应用程序

4

我有一个继承了Application类的类。在这个类中,我正在检查互联网连接并调用网络服务。

下面是我用来检查的方法:

public static boolean isInternetConnected(Context mContext) {
        ConnectivityManager connec = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connec != null
                && (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED)
                || (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)) {
            return true;
        }
        return false;

    }

当没有互联网连接时,我想强制关闭应用程序。如何做到这一点?
我有一个替代方案。如果没有网络连接,我可以跳过调用api进程,并等待第一个活动开始并尽快完成该活动。
可以在Application类中实现吗?

为什么你强制关闭应用程序?你可以简单地关闭/退出应用程序。 - Sree
在 Application 类中如何关闭/退出应用程序? - Nitesh Kumar
http://androidbasic-answer.blogspot.in/2012/02/how-to-exit-from-android-application.html - Sree
@Sree 我无法在Application类内调用finish()方法。你发送的链接是在activity类中关闭应用程序。 - Nitesh Kumar
在应用程序类中使用标志退出,并在主活动中调用finish()。 - Sree
3个回答

4
您可以在派生的应用程序类中调用它;
 android.os.Process.killProcess(android.os.Process.myPid());

0

你为什么想要强制关闭一个应用程序?我第一次听到这样的请求。

当没有网络时,你可以像这样关闭活动:

if (!isInternetConnected(context)){
    finish();
}

是的,我可以关闭Activity,但我认为你误解了我的意思。我当前在Application类中而不是在Activity中。我无法在Application类中调用finish()方法。 - Nitesh Kumar

0

最好使用这个...

public boolean isOnline() 
{
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting())
    {
        return true;
    }


   // Toast.makeText(getBaseContext(), "Internet is not Connected", Toast.LENGTH_LONG).show();
    alertwifi();
    return false;
}





 public void alertwifi()
    {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);

        // set title
        alertDialogBuilder.setTitle("NO Internet Connection!!");

        // set dialog message
        alertDialogBuilder
            .setMessage("Click below to turn on Wifi or Enable Data pack")
            .setCancelable(false)
            .setPositiveButton("Wifi",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close
                    // current activity
                    wifi();
                }
              })
               .setNeutralButton("GPRS",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close
                    // current activity
                    datapack();
                }
              })
            .setNegativeButton("Back",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing

                    exit();


                }
            });





public void exit()
{
     Intent intent = new Intent(Intent.ACTION_MAIN);
     intent.addCategory(Intent.CATEGORY_HOME);
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(intent);
}


// create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接