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.
Related
I have and android application in which I am initializing a third party library in the onCreate() of the main activity. Now when there is any runtime error in initialization of the library, My application will crash and at that point I want to show an AlertDialogue to the user saying "some error message" in the dialogue box.
The problem is that when the application crashes, my alert dialogue is showing up but it is dismissed along with the application. I want it to be persisted even when the application crashes.
Below is the code of alert dialogue which I used.
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Error!")
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
alert.show();
Can anyone guide me in how to make that AlertDialogue independent of the activity, so that it doesn't require the app window. If not possible, then what other widgets, I can use or customize?
If you want your app to keep running, avoid crashing.
If you want special handling for issues in a library init, catch the relevant exceptions and keep the app running in a state that shows the dialog and then does something else, such as finish()ing the activity that had a problem in its init.
To avoid the 3rd party library from taking down your app, wrap its API calls in a try/catch block, e.g.:
try
{
BuggyLibrary.init(); // example
}
catch (Exception e)
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Error!")
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
alert.show();
}
You'll need to consider the other implications of this library not initializing ... if your app can't continue functioning without it you'll need to gracefully finish() your Activity when the dialog is dismissed.
May be it's possible to achieve something like that.
There is a library that allows you to show activity when your app crashes. Basically it launches new activity in another process.
At the same time you can show activity as a dialog by using Theme.AppCompat.Dialog for your activity as a theme.
Perhaps combining these two approaches will get what you want.
P.S. Or by using old fashioned try-catch as some people suggest.
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm trying to build activity aware app.
I used the activity recognition example from android developers as starting point.
In this example the moment the user starts moving notification appears with request to turn on the GPS.
I'm trying to replace this notification with an alarm dialog box and some sound.
the simple and most common alert box that I found is not working for me since it is imposible to create an allert from service.
Thanks to Harsh for giving me an idea of activity.
I will post the code the moment it will work.
Thank you in advance
as promised the working code of an alert box invoked from service :
1) the code in the service file:
Intent intent;
intent = new Intent(this, MyAlert.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
2) the code in the alert activity .java file :
public class MyAlert extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_alert);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Your Message")
.setCancelable(false)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
stopService(getIntent());
dialog.cancel();
finish();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
3)alert activity manifest was left empty of code, just a layout
4)the manifest.xml:
<activity
android:name="com.igor.map.MyAlert"
android:theme="#android:style/Theme.Dialog">
</activity>
All works, one thing still has a problem, when i press the Ok button the allert closes but the label of the app stays until I click on screen.
Start an Activity from service, and declare activity as dialog in your manifest. Like this
<activity
android:name="com.example.DialogActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog">
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;
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.