Why do you wish to force close an application? Heard of such request for the first time.
When there is no internet, you can just close the activity like this:
if (!isInternetConnected(context)){
finish();
}
Domanda
I have a class which extends Application class. In this class I am checking Internet Connectivity and calling the web service.
Below is the method I am using to check:
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;
}
When there is no Internet connection I want to force close the application. How to do it?
I have an alternative solution. If there is no Internet connection, I can skip calling api process and wait for the first activity to start and finish that activity as soon as it starts.
Is it possible to do it in Application class?
Soluzione 2
Why do you wish to force close an application? Heard of such request for the first time.
When there is no internet, you can just close the activity like this:
if (!isInternetConnected(context)){
finish();
}
Altri suggerimenti
You can call in Application derived class ;
android.os.Process.killProcess(android.os.Process.myPid());
This is better use this...
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();
}