How to know if Android Wifi settings pane is on top? - android

I need to know if the Wifi settings pane is on top. I found this way:
ActivityManager a = (ActivityManager)context.getSystemService( Context.ACTIVITY_SERVICE );
List<RunningTaskInfo> t = a.getRunningTasks( 1 );
if ( t.get( 0 ).topActivity.getClassName().equals( "com.android.settings.Settings$WifiSettingsActivity" ) )
{
return true; // Yes it is!
}
else
{
return false; // No it's not!
}
Actually, it works fine. But in Android reference doc here it is written that we should not use this method (read the link for more details). So does somebody know how to perform this check in an other way? Note that I target SDK level 14 and higher.
Thanks!

Don't detect the settings pane, rather focus on why you would want to open the settings panel (you need WiFi connection and it is disabled, you need a particular SSID and the current one is not that one, ...).
For example, show your activity and if WiFi is disabled, show a popup to ask the user what he wants to do.

Related

android open battery settings programically

I'm looking for a way to open the battery settings screen from an android app.
So far I found the two intents :
Intent.ACTION_POWER_USAGE_SUMMARY
Settings.ACTION_BATTERY_SAVER_SETTINGS
but none of them open this screen.
I was wondering if anyone knows of such a way. It sounds strange that an intent for something so simple doesn't exist
Settings.ACTION_BATTERY_SAVER_SETTINGS on "plain" Android versions will show the settings page you want to show.
Intent.ACTION_POWER_USAGE_SUMMARY will lead to the overview page showing the battery consumption.
Some manufactures such as Samsung build their own implementation over the system one, e.g. in this the "Battery" page. On Samsung devices, you can call this by calling the SmartManager interface directly. An code example:
if (Build.MANUFACTURER == "samsung") {
val intent = Intent()
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
intent.component = ComponentName("com.samsung.android.lool", "com.samsung.android.sm.ui.battery.BatteryActivity")
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
intent.component = ComponentName("com.samsung.android.sm", "com.samsung.android.sm.ui.battery.BatteryActivity")
}
try {
activity?.startActivity(intent);
} catch (ex: ActivityNotFoundException) {
// Fallback to global settings
startActivity(Intent(Settings.ACTION_SETTINGS))
}
} else {
startActivity(Intent(Settings.ACTION_BATTERY_SAVER_SETTINGS))
}
It can be the case that you need additional cases for Huawei or Xiaomi as well.
Huawei can be "com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"...
...and the MIU based ones "com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"
I know this is quite old. But a trick I use is going to the appropriate settings screen in the device settings and then while connected to the phone run:
adb shell
dumpsys window windows | grep -E 'mCurrentFocus'
This returns the package name and Activity name currently in focus.
Using that I can check in code if the intent is callable. If it is, I launch it. If it isnt, I might have better luck with a different screen that is near by or explain to the user he needs to do something manually etc... Obviously the more devices you have, the more Intents you can create and check at run time. Im sure there is a list of Intents for different devices online.

Set Screen Lock Setting in "None"

I need to set Screen Lock Setting to "None" in the android system.
I am deploying an app that is being sent out to users on pre-configured tablets.
This pre-configuration is done manually by the tablet supplier.
The settings to pre-configure is to set the Screen lock to None when the app is installing.
I don't think this is possible, the user has to do it them self.
Considering all apps (With a similar requirment) I have used so far send me to the "Select lock screen" screen & ask me to disable it manually.
I could be wrong tho!
Try this:
try{
Class lockPatternUtilsCls = Class.forName("com.android.internal.widget.LockPatternUtils");
Constructor lockPatternUtilsConstructor =
lockPatternUtilsCls.getConstructor(new Class[]{Context.class});
Object lockPatternUtils = lockPatternUtilsConstructor.newInstance(getContext());
Method setLockScreenDisabled = lockPatternUtils.getClass().getMethod("setLockScreenDisabled", boolean.class ,int.class);
setLockScreenDisabled.invoke(lockPatternUtils, new Object[]{true,0});
}
catch(Exception e){
Log.e(this.toString(),e.toString());
}
if you want change lock to swipe:
setLockScreenDisabled.invoke(lockPatternUtils, new Object[]{false,0});
but you need to change your app to system uid:
android:sharedUserId="android.uid.system"

Android 5.0 (L) - Check data Roaming Setting

I have a problem identifying the data roaming setting in Android L. In previous versions of Android, I was able to access either Settings.Secure or Settings.Global (depending on the Android version), and get the setting.
But now, on Android L, this no longer works. Whether data roaming is on or off, the return from the Settings.Global is always 0.
Android L supports multi SIM out of the box, so, a new manager was created to handle this: SubscriptionManager. This subscription manager, handles the several settings of the several SIM cards in the form of SubInfoRecord classes. I can retrieve the settings per SIM card.
However, the dataRoaming filed inside that class is always 0 as well.
Does anyone know how can this be achieved on the new API?
My app is a system app that comes embedded in the phones from factory so, I should be able to access all the APIs available.
However, I've spent a long time looking in the source code but I found nothing. In the Settings.Global class there's no indication that that setting no longer works on Android.
Does anyone have a clue on where this setting was moved to?
Thanks in advance!
Check this DevicePolicyManager.setGlobalSetting
as from documentation this can only be called by device owner app.
Is your app is installed as device owner ?
If not you can check the following links
Create device owner without root
Create device owner with root
Do something like this
DevicePolicyManager manager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
manager.setGlobalSetting(<Admin_Component>, Settings.Global.DATA_ROAMING, <value>);
Admin_Component: Component instance
Value: "0" for disable or "1" for enable
Since android 5.0, android supports multiple SIM cards, use the following code to check for data roaming.
public static boolean isDataRoamingEnabled(Context context) {
SubscriptionManager subMngr = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
int id = SubscriptionManager.getDefaultDataSubscriptionId();
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
return false;
}
SubscriptionInfo ino = subMngr.getActiveSubscriptionInfo(id);
if (ino == null)
return false;
return ino.getDataRoaming() == 1;
}

Showing WiFi setting and selecting one network among the list

I'm able to send the user to the WiFi settings when it is necessary in my application:
Intent wfintent = new Intent( Settings.ACTION_WIFI_SETTINGS );
wfintent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
this.startActivity( wfintent );
So far, so good...! Now, among the list of WiFi networks displayed, I know exactly which one I want to connect to (by using WifiManager.startScan(), .getScanResult(), no problem with that) and I can also know if this network was previously configurated (given a password) (by using WifiManager.getConfiguredNetworks()).
The problem is that I would like not only to open and display the WiFi settings pane but also select the network and open the dialog that prompt for the password.
Is it possible to do that directly with the intent I previously gave in example?
Thank you for your help!
No, the WifiSettings only make a call to WifiManager.connect in PreferenceFragment.onPreferenceTreeClick, Fragment.onContextItemSelected, and DialogInterface.OnClickListener.onClick. There is no Intent extra it looks for, or any other way to select a connection using the Intent alone.

Unknown sources settings android

The issue is that I need to install an apk(non market app) and for this, the user need to activate the unknown source setting, so i send him (if he didn't have it activated) to the settings so he can turn on the option, the issue is that i tested it in different phones and in samsung that option is on applications while in htcs phones is on security. i want send the user to that option but i don't know how to do it
I read about this and no one knows exactly how to do it
this is my code
int canInstallFromOtherSources = Settings.Secure.getInt(ctx2,Settings.Secure.INSTALL_NON_MARKET_APPS);
if(canInstallFromOtherSources == 0)
{
Intent intentSettings = new Intent();
intentSettings.setAction(android.provider.Settings.ACTION_APPLICATION_SETTINGS);
startActivity(intentSettings);
}
You can do it with the following line (changing to the corresponding action):
startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), REQUEST_CODE_ENABLE_LOCATION_PROVIDERS);
Check Android Settings documentation.
I think you should use ACTION_SECURITY_SETTINGS and one of ACTION_APPLICATION_SETTINGS or ACTION_APPLICATION_DEVELOPMENT_SETTINGS.
And here (line 304), you've got a working example of one of my apps: Tureame

Categories

Resources