my question is on an Android phone how can I check if the usb debugging flag is enabled or not programmatically?
in my application I want to show the status of usb debugging and I want to get it programmatically
How can I get if usb debugging is enabled programmatically?
Try this:
if(Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.ADB_ENABLED, 0) == 1) {
// debugging enabled
} else {
//;debugging does not enabled
}
Simple:
boolean isDebuggable = ( 0 != ( getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE ));
And if you want to check if it is connected:
if (!Debug.isDebuggerConnected()){
//Yes, it is.
}
Related
After reading through a bunch of stale guides and stackoverflows, I was able to usb adb to install an apk as a system app in /system/priv-app that successfully toggles AirplaneMode in Android oreo:
// method in Activity, called via click listener on a Button
private void setMobileRadioEnabled_Option1(boolean enabled) {
android.content.Context context = this;
int value = enabled ? 0 : 1;
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
Settings.System.putInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, value);
} else {
Settings.Global.putInt(
context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, value);
}
}
Permissions in AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
Standard release build in AndroidStudio with custom signing keys, installed via:
adb root
adb remount
adb push app-release.apk /system/priv-app
adb shell chmod 644 /system/priv-app/app-release.apk
adb reboot
On reboot, the app is installed and I can run it without issue.
I check in the notifications drawer / status bar what things are like to start with:
I then click my Button in the app, and check what happens:
As you can see, airplane mode seems to be successfully enabled based on the status of the airplane mode icon. But wifi and cellular data continue to be connected, and the status bar doesn't replace the text "Android" with "Airplane mode". In this state, if I hop over to chrome, I can clearly load websites I've never visited before. So airplane mode doesn't in fact seem to be actually on.
What am I doing wrong? I expect turning on airplane mode via System.putInt() to have the same effect as tapping the airplane mode tile in the status bar. No exceptions or useful error information spitting to logcat when I execute the code.
Checking this answer it seems that you need to send a broadcast to notify that you changed the airplane mode.
The broadcast should be:
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", < current value of the airplane mode >);
sendBroadcast(intent);
Alternatively to jonathanrz's additional code above, which I believe is closer to canonical, I found that the following worked in place of the Settings.System.putInt() code and did not require sending the intent (or adding the permission(s) necessary to send it), at least on Oreo. I created it by merging a few answers and offhand comments from other posts, particularly an answer sketch hidden in a comment by "Navas pk" on Toggle airplane mode in Android:
private void setMobileRadioEnabled_Option2(boolean enabled) {
try {
final ConnectivityManager mConnectivityManager = (ConnectivityManager) getSystemService(android.content.Context.CONNECTIVITY_SERVICE);
final Class mClass = Class.forName(mConnectivityManager.getClass().getName());
final Method setAirplaneMode = mClass.getDeclaredMethod("setAirplaneMode", Boolean.TYPE);
setAirplaneMode.setAccessible(true);
setAirplaneMode.invoke(mConnectivityManager, !enabled);
} catch (Exception e) {
e.printStackTrace();
}
}
I have an app that connects to a usb accessory. Simple, but the accessory stays connected even after the usb cable was unplugged.
This is my code:
public boolean checkOpenAccessory(){
UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
UsbAccessory[] accessories = usbManager.getAccessoryList();
UsbAccessory accessory;
if(accessories == null ){
accessory = null;
return false;
} else {
accessory = accessories[0];
if(usbManager.hasPermission(accessory)) {
return (usbManager.openAccessory(accessory) != null);
} else {
return false;
}
}
}
This returns me that I am connected to a usb accessory, but I am not. I have a developer app that monitors everything in the phone and says the same thing:
UsbAccessory[] accessories = usbManager.getAccessoryList();
Accessory is connected. I also tried something like triggering a broadcast or kill background processes. The phone it will show no accessories only if I restart it.
sendBroadcast(new Intent(UsbManager.ACTION_USB_ACCESSORY_DETACHED));
My problem is only a Htc One M9, Api 22, Android version 5.1 .
If you guys can help it would be a great thing, I spent a few days trying to figure it out.
I did some improvements.It works something like this.. but not 100 % sure.
boolean check = checkOpenAccessory();
And then use checkOpenAccessory() when is needed somewhere and will return the real thing.
Basically the second time will work, not 100% sure but it work.
Refer to this link.
Is there any way to uncheck the option "Do not keep activities " from developer settings in the code of my application? I want to check the status and uncheck this option for my application. Is this possible?
You should not rely on that setting, activities can be destroyed even if that setting is turned off (for instance, if the device is running low on memory).Also, you can not disable / enable the setting programatically.
You can, however, check if the setting is turned on, with this code:
boolean enabled;
if (Build.VERSION.SDK_INT >= 17) {
enabled = Settings.System.getInt(context.getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) == 1;
} else {
enabled = Settings.Global.getInt(context.getContentResolver(), Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0) == 1;
}
I have seen this question : check phone settings for haptic feedback but I don't understand the solution (for me it does not work).
Is there another solution ? I did not find anything on the javadoc.. ?
Thanks
You can add this method to any of your classes and use it to check whether haptic feedback is enabled.
public static boolean isHapticFeedbackEnabled(Context context) {
int enabled = Settings.System.getInt(context.getContentResolver(),
android.provider.Settings.System.HAPTIC_FEEDBACK_ENABLED, 0);
return enabled != 0;
}
The method goes into system settings database and checks value of "haptic_feedback_enabled" setting. If there is 0 then haptic feedback is disables, otherwise it is enabled.
I wanted to know if there is any way to check programmatically in android phone whether DHCP is enabled or disabled. (Assuming its possible to disable DHCP through some way or dhcpd is not running)
You can do the following to know if Android is currently using static IP adresses or DHCP attributed ones :
try {
if ( Settings.System.getInt(getContentResolver(),
Settings.System.WIFI_USE_STATIC_IP) == 0 )
/* Static ip addresses mode is not enabled */
;
} catch (SettingNotFoundException e) {
/* Do something smart here */
}
However, note that this method seems now deprecated and that it does not work across all devices (AVD for example).