android - how to click on System Dialog options programmatically - android

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

Related

android onresume - conflict with AlertDialog

In my app I am using location services, and after first install the app asks for Location permission. If the user click OK, permission is granted, if Cancel, then I have another dialog with some info.
Then - if the user has turned off the GPSon his device, a dialog will come up which asks to enable GPS - if Ok is clicked, the device settings are opened and here the user can enable the GPS.
As far as now everything works fine. But I need to restart the activity after the user is back from settings. (So I can load some items according the location).
For this I used onresume():
#Override
protected void onResume() { //restart activity after back from GPS settings
String action = getIntent().getAction();
// Prevent endless loop by adding a unique action, don't restart if action is present
if(action == null || !action.equals("created")) {
Intent intent = new Intent(this, Okoli.class);
startActivity(intent);
finish();
}
// Remove the unique action so the next time onResume is called it will restart
else
getIntent().setAction(null);
super.onResume();
}
I used there a unique action to avoid loop restart, so in oncreate I am setting also getIntent().setAction("created");
Now this is working fine - the activity restarts after the user is back from settings, but it conflicts with Permission dialog which I mentioned as first.
So if I have the onResume function, and the user installs the app, the Location permission dialog comes up, but in this case the user can't click CANCEL, because the dialog is looping forever if he clicks cancel. So it is appearing again and again until he clicks OK.
When I remove the whole onResume section from my code, then the Permission dialog works fine, but I need onresume for restarting activity.
okey, finally I store a value to SharedPreferences - when user doesn't allow the location access, and then I check this value onResume and only restart the activity if the value is not set. Works fine!

Getting user's answer (System dialog - Android)

I am a newbie on Android and I am creating a launcher. I want apps to be removed (uninstalled) so I have a list and I invoke system to uninstall it.
How can I know if the user pressed "Cancel" or "Ok" in the system dialog?
(I know the system will unisntall the app if I press "Ok" or wont if I press "Cancel", I just need to know how to get the answer to remove or not the app of my list [ArrayList]).
If you cant know it, how can I do to remove an app from a list without knowing if the user is going to uninstall it or not?
public void uninstall (int position){
Uri package1 = Uri.parse("package:"+apps_block.get(position).name.toString());
Intent uninstall = new Intent(Intent.ACTION_DELETE, package1);
startActivity(uninstall);
AppDetail toRemove = adapter_block.getItem(position);
adapter_block.remove(toRemove);
}
With this code, the app is always removed from my list even I press "Cancel".
You are removing the item from the list immediately after startActivity(). The user has not even seen the dialog yet by that point.
You could listen for the ACTION_PACKAGE_REMOVED system broadcast, confirm that it was for your requested package, and remove the package from the list at that point. By doing this from your activity via registerReceiver(), you can find out fairly quickly and have easy access to your UI code to update the list.

Screen Overlay Detected | (Android 6.x) Lenovo, Redmi & Samsung Devices

I have created an app which continuously run in background and show an floating overlay TextView(By using the permission android.permission.SYSTEM_ALERT_WINDOW).
I have problem in Lenovo android devices, when other applications try to request for user permission there is an alert dialog says "Screen Overlay Detected". While I have test the same application on Redmi 3S Prime device the "Allow" button in permission dialog is not clickable, until I have turned off the Floating Overlay TextView in my application.
So is there any solution to resolve this device specific issue? One of the possible solution may be disable floating TextView while permission dialog is visible to user and show floating overlay when permission dialog is closed, but the problem is how can I detect the permission dialog open/close state for other foreground application from my app.
Please suggest...
Unfortunately it seems that this is inherent to the Android OS, and as far as I know there is no way to detect when another app is requesting permissions. The general flow for interacting with other apps is to send intents to them in a push manner, so theoretically it could be done if the other apps send an intent to your app to disable the overlay, though this is not a practical solution.
I could be completely wrong, but I am yet to see a programmatic solution to this problem. The best you can probably do is warn your users that your app may cause this problem, and provide them with a quick temporary disable button.
how can I detect the permission dialog open/close state from my app??
Implement Method Mentioned Below, to run this check at the onCreate of the first Activity
public final static int PERM_REQUEST_CODE_DRAW_OVERLAYS = 1234;
/**
* Permission to draw Overlays/On Other Apps, related to
'android.permission.SYSTEM_ALERT_WINDOW'
in Manifest
Resolves issue of popup in Android M and above "Screen overlay detected- To change this permission setting you first have to turn off the screen overlay from Settings > Apps"
If app has not been granted permission to draw on the screen, create an Intent &
set its destination to
Settings.ACTION_MANAGE_OVERLAY_PERMISSION
&
* add a URI in the form of
"package:"
to send users directly to your app's page.
Note: Alternative Ignore URI to send user to the full list of apps.
public void permissionToDrawOverlays() {
if (android.os.Build.VERSION.SDK_INT >= 23) { //Android M Or Over
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, PERM_REQUEST_CODE_DRAW_OVERLAYS);
}
}
}
Called on the activity, to check on the results returned of the user action within the settings
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PERM_REQUEST_CODE_DRAW_OVERLAYS) {
if (android.os.Build.VERSION.SDK_INT >= 23) { //Android M Or Over
if (!Settings.canDrawOverlays(this)) {
// ADD UI FOR USER TO KNOW THAT UI for SYSTEM_ALERT_WINDOW permission was not granted earlier...
}
}
}
}
NOTE:Above code and extract is taken from following gist.
Hope this Helps!!!..
Just clear data of the Es explorer from device then after restart device.
I think these two links could help you.
firstlink
secondlink
Try to use for debug build -- targetSdkVersion 22.
After signed APK (for all targetSdkVersion(s)) application will work fine.

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 );
}
}

Closing android system dialog box automatically

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);
}
}

Categories

Resources