How to close an app after showing a dialog? - android

I have almost completed my app development and I have even allowed a way to show up a toast message if there is no internet connection. I need to know how to close an app after showing a dialog if there is no internet connection?
My app is a webview app. So when user turn on the app, the app must check for internet connection. and display a dialog box if no network and the app must close after showing dialog.
OR
I don't want users to see the "webpage not available" page, so if there is no internet connection, the app must close after showing a dialog box with one button 'close'. Or is there any way to redirect users to a text in directory which shows no internet connection?
I need those code only; all the other codes have been done and are working properly.

Just invoke finish() to destroy activity in the onClick() function of dialog. Like:
...
builder.setMessage('There is no connection!! Please close the activity!')
.setPositiveButton('close', new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
...

You can popup a dialog after checking the internet connectivity as follows
new AlertDialog.Builder(this)
.setTitle("Connection Error")
.setMessage("You are not connected to Internet.")
.setPositiveButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// finish the activity here or,
// redirect to another activity
}
})
.show();

You can check your internet connectivity before setting the view to the activity.
Like :
if(connnected){
setContentView(layoutId);
}else{
show a tosat with information no internet;
finish activity.
}

call finish() method on your activity once user clicks on dialogue

Related

Displaying Toast after opening a Settings Activity

I'm trying to display a Toast right after I open a Settings Activity, which should contain a few instructions on what to do in the opening screen.
For example, let's say I want the user to connect to a specific network, so in my code I run
Toast.makeText(context, "Instruction message", Toast.LENGTH_SHORT).show()
startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
to display a Toast followed by a call to open up the WiFi Settings Activity. The problem here is that the Toast will display in the current Activity inside my app, but very quickly disappear as soon as the new Activity comes up.
I'm wondering if there's a way of displaying that same Toast but on the opening Activity, rather than the one that is doing the call, in order to properly instruct the user on what to do on such new Activity.
Or perhaps there's a better way of achieving this?
I think it would be better if you used a dialog, so after clicking OK, the user will be shown the screen to change WiFi, like so:
AlertDialog.Builder(context)
.setTitle("Your app name")
.setMessage("Please turn on WiFi on the following screen.")
.setPositiveButton("OK", DialogInterface.OnClickListener { dialog, id ->
{
startActivity(Settings.ACTION_WIFI_SETTINGS)
}
})
.setNegativeButton("Cancel", null)
.show();
You can read more about Dialogs here: https://developer.android.com/guide/topics/ui/dialogs

How can i detect the change in network state and reload my android app if the connectivity is available

I have an android app which runs when internet connection is active. When the connection is not available, a dialog box appears through which wifi can be activated. I have made the app to quit when connection is not available after initiating wifi connection. The question is, i have to reload the app and continue using the app when the internet connection is available after enabling the wifi rather than to exit the app.
Here is the code:
if (CheckConnection.checkInternetConnection(this)) {
//load a webview and show contents from web.
}
else
{
try {
AlertDialog alertDialog = new AlertDialog.Builder(Activity.this).create();
alertDialog.setTitle("Info");
alertDialog.setMessage("Internet Connection not available. Please cross check your internet connectivity and try again.");
alertDialog.setIcon(R.drawable.error);
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setCancelable(false);
alertDialog.setButton2("WiFi Setting", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.settings", "com.android.settings.wifi.WifiSettings");
startActivity(intent);
System.exit(0);
}
});
alertDialog.show();
}
catch(Exception e)
{
}
Don't call System.exit(0).
First of all because it is not a recommended way of quitting your app and makes it look to the user as if it crashed.
Second, why are you exiting at all? The user will be navigated to the internet settings, switch them on and when they press back, your activity will already be there. You can recheck internet status onResume of your activity. This is the acceptable way of handling an unavailable internet connection in your application.

Is there a "Wrong password on lock screen" action intent?

I'm looking for a way to get a notification if a user enters a wrong password on the Android lock screen (the system lock screen - not a lock screen in my own app). Is there an existing intent that get's fired in that situation?
Many thanks in advace.
I'm looking for a way to get a notification if a user enters a wrong password on the Android lock screen (the system lock screen - not a lock screen in my own app). Is there an existing intent that get's fired in that situation?
If you have implemented a DeviceAdminReceiver using the device admin APIs, it will be called with onPasswordFailed().
You can create your own dialog in android app to display the error message.Below is the code for that:
AlertDialog alertDialog = new AlertDialog.Builder(LoginActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Error");
// Setting Dialog Message
alertDialog.setMessage("Not A User");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.inputerror);
// Setting OK Button
alertDialog.setButton("OK",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
// Write your code here to execute after dialog
// closed
Toast.makeText(getApplicationContext(),"Not A Valid User", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
return;

Android Eclipse, AlertDialog outside of ativity

I have an AlertDialog with an OK button in my activity by using this code:
alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
//Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome to AndroidHive.info");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.icon);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
}
});
alertDialog.show();
This works fine within the application, but I want this to be ran and show even if we're outside of the application. Of course with the application still running and not closed. Much like Toast. even if I can use a Toast with an OK button I'd be happy.
Any help is appreciated. Thanks.
You can't run a dialog as standalone. You will have to get an Activity to display your dialog. In my mind, your solution would lead to a bad user experience if every app could launch a modal dialog whenever it wants.
But you can imagine 2 solutions :
bringing up an activity with this dialog open (maybe you could make the activity transparent) when a certain intent is sent to the system.
use notifications, it looks quite close from what you want to do.

Create notification when application is in background, Android

Initial description:
My application handles some events and do some task related to that event, even if application is in background. I am using receivers.
Problem description
I want to show notification when an event starts or end.
What I did yet
I used Notification manager :- But I need to show notification on home screen not on notification bar.
I used App widget :- But user will not able to notice any changes in information on app widget easily, because this does not do any eye catching effect.
I used Toast :- But this having time limitation to show, I want to show notification until user close.
So what should be the option? How can I do my task?
Please refer below image, as what I want
Unfortunately, you've explored all the possible options. What you want to achieve is not provided intentionally because it will make for very bad user experience. Imagine a notification (of the kind that you want) popping up when you are looking at an email!
Attention piracy
When your app is in the foreground, you can do a lot of things.
When your app is not in the foreground, there are only two options
Start an activity: You must do this only if essential (like an incoming phone call activity). If the user deems it more irritating than useful, your app is going down the drain!
Use the notification manager.
An option is to use Alert Dialogs
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
Or you can use run an Activity with the Dialog theme.

Categories

Resources