Closing android system dialog box automatically - android

Is there a way to close the android system dialog box automatically from the app code?? Or else to click a particular portion of a system dialog box when it appears?? I am trying to write a calling app and in that when someone tries to call and if the airplane mode is "on" a system dialog will appear asking to turn off the airplane mode. I want to know whether it is possible to click "ok" in this dialog box automatically...
Thanks in advance...

onWindowFocusChanged method is called when focus is changed. So, we can use that method to disappear system dialog.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if(!hasFocus) {
// Close every kind of system dialog
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
}
}

Related

Android dismiss a system dialog programmatically

I want to dismiss a system generated alert dialog programmatically. I have tried all solutions provided here(stackoverflow) but it does not seem to work.
This is the accepted answer mostly everywhere, but it only dismisses notification panel and recent tasks menu.
I have tested it on devices with os version 4.0.3, 4.2.2, 4.4.2 and 5.1.1, it has the same behavior on all of them. There are apps which can actually dismiss all system dialogs (Mubble). Can someone suggest how it is done?
Thanks
The usual answer to this is
sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
However, this does not work for everything. In particular, on some devices it does not work for the "recent apps" list.
The solution is to detect when your app loses focus and then move your app to the front. Note that your app will need the permission android.permission.REORDER_TASKS to do this.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
this.keepFocus = true;
}
if (! hasFocus && this.keepFocus) {
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
am.moveTaskToFront(getTaskId(), ActivityManager.MOVE_TASK_WITH_HOME );
}
}

android - how to click on System Dialog options programmatically

I need to answer to the YES/NO system dialog programmatically
for example this code closes any dialog that comes to the screen
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.e("Focus debug", "Focus changed !");
if (!hasFocus) {
Log.e("Focus debug", "Lost focus !");
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
}
}
is it possible to do something else ?
to click YES or NO on that dialog ???
I searched for something different from ACTION_CLOSE_SYSTEM_DIALOGS but I got nothing
I might need to catch the dialog first .. then I need to click yes when the phone shows yes/no dialog asking the user to enable the bluetooth
is it possible?
In the API docs it says this about the ACTION_CLOSE_SYSTEM_DIALOGS action:
Broadcast Action: This is broadcast when a user action should request a temporary system dialog to dismiss.
You are simply requesting that the dialog be dismissed and therefore it is up to the dialog to accept the request or not. What this means is that some (most?) dialogs will not be dismissed. Android is setup to sandbox applications and any interaction with system components will be limited at best. I haven't been able to find any further information on how you may be able to request that a dialog accept a specific option, but I would suggest that if it's at all possible, then it will only work on very specific system dialogs.
You can do it with ADB touch events. First you need the location of touch:
adb shell getevent -l
Then you need to send the event:
adb shell input tap <x> <y>
You can also run adb commands programmatically (just remove the adb shell part):
Runtime.getRuntime().exec("input tap <x> <y>");
You can see how to get values from the shell programmatically here:
https://stackoverflow.com/a/66499406/804894

OnActivityResult called prematurely with Settings menu

In my Application, before going into the App, I have to check my wifi connection and take the user to the wifi settings if wifi is not enabled.
I dont want to use WifiManager.setWifiEnabled(); as I want to give the user the opportunity to set it.
I have referred the link, onActivityResult() called prematurely
and also, onActivityResult and the Settings Menu
But it is not working for me. The OnActivityResult() and onResume() is called almost at the same time I get into the Settings menu.
Here is my code,
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("You are not currently connected to any network. Please turn on the network connection to continue.")
alert.setPositiveButton("Settings", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int arg1)
{
Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
startActivityForResult(intent,SETTINGSCODE);
dialog.cancel();
}
});
alert.show();
In the onActivityResult(), I am checking the network state again which is called before changing the settings.
What can i do so that OnActivityResult()
will be called only after coming back from the settings menu?
Please help me with this issue..!!
This is how I solved this issue.
In reference to the answer posted for a similar question, I changed startActivityForResult() to startActivity() as the order of the action calls is like
startActivityForResult()
onActivityResult()
onCreate() (new activity)
setResult() or finish()
The control is taken to the settings page where the user can switch on/off the Wi-Fi and later come back to the app. :)

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.

Categories

Resources