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
Related
I've been searching on this for quite some time, and have found lots of solutions that ultimately use the Android GUI to prompt the end-user for connection/confirmation. We would like to present a user with a form and directly connect to the specified network. I understand from a security perspective why this is necessary, but in our case, it is a system app going on a fully managed Android device. It is not intended for general use in the Play store. We are using Xamarin, but I can adapt any Java/Kotlin code.
The closest I've found thus far is the following (using C#):
public void ConnectToWifi(string ssid, string password = "") {
var wifiNetworkSpecifier = new WifiNetworkSpecifier.Builder().SetSsid(ssid);
if (!string.IsNullOrEmpty(password))
wifiNetworkSpecifier.SetWpa2Passphrase(password);
var networkRequest = new NetworkRequest.Builder().AddTransportType(TransportType.Wifi)?
.SetNetworkSpecifier(wifiNetworkSpecifier.Build())?.Build();
if (networkRequest is null)
return;
var theNetworkCallback = new TheNetworkCallback();
var connectivityManager = (ConnectivityManager?)MainActivity.Current.ApplicationContext?
.GetSystemService(Context.ConnectivityService);
connectivityManager?.RequestNetwork(networkRequest, theNetworkCallback);
}
It sorta works, but does prompt the end-user, and my understanding, this approach is deprecated and doesn't work well in newer versions of Android. We're hoping for a solution that works in Android 11.
I'm even fine if there's a solution to write directly to wifi files on the OS. I've seen various solutions to manually populate entries via ADB, but I'm having a tough time adapting that to Xamarin/Java (can't seem to access the /data/misc/wifi directories). Again, this is intended for use exclusively on our own managed devices.
I have a blog post about this topic here: https://blog.ostebaronen.dk/2020/11/android-11-wifi.html
Android Network API is not the greatest thing to work with as there are pitfals depending on the API level the code runs on.
From Android 10 and up a lot of the Network stuff has been restricted for "privacy" reasons, so you cannot work around not asking the user for input, unless the device is rooted or your App is set up as Device Admin.
For Android 11, there is a new API to present a system dialog and allow the user to save and connect to a network. This will look something like:
You can launch this through an Intent called android.settings.WIFI_ADD_NETWORKS:
var intent = new Intent(
"android.settings.WIFI_ADD_NETWORKS");
var bundle = new Bundle();
bundle.PutParcelableArrayList(
"android.provider.extra.WIFI_NETWORK_LIST",
new List<IParcelable>
{
new WifiNetworkSuggestion.Builder()
.SetSsid(ssid)
.SetWpa2Passphrase(password)
.Build()
});
intent.PutExtras(bundle);
StartActivityForResult(intent, AddWifiSettingsRequestCode);
You can then get the result in your Activity overriding OnActivityResult and fetching the result like so:
if (requestCode == AddWifiSettingsRequestCode)
{
if (data != null && data.HasExtra(
"android.provider.extra.WIFI_NETWORK_RESULT_LIST"))
{
var extras =
data.GetIntegerArrayListExtra(
"android.provider.extra.WIFI_NETWORK_RESULT_LIST")
?.Select(i => i.IntValue()).ToArray() ?? new int[0];
if (extras.Length > 0)
{
var ok = extras
.Select(GetResultFromCode)
.All(r => r == Result.Ok);
// if ok is true, BINGO!
return;
}
}
}
I have a repository here with the full sample: https://github.com/Cheesebaron/Android11WiFi
From using this in the wild, I've found that this API does not work nicely with some OEMs such as OnePlus and Huawei. Some of these either restrict or the System Settings App simply crashes due to a misconfiguration on their part. For those I fall back to the API's introduced in Android 10.
My Goal is here is to set my app as default launcher on Huawei devices.
1 - Explanations:
1.1 - Current situation:
I am already able to:
Check if my app is the default launcher
Display the 'launcher picker' (with the 'use once' / 'always' choice)
This all works fine.. except on Huawei devices!
From my point of view, Huawei's Android flavor does not properly 'honor' the "ACTION_MANAGE_DEFAULT_APPS_SETTINGS" intent action contract.
// this displays the list of default apps on all tested devices, except on Huawei devices!
// instead, it does display apps permissions, app links and apps'advanced settings
intent.setAction(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS);
activity.startActivity(intent);
As a B Plan, I am able to display the 'Applications & Notifications' settings 'page' using this:
String packageName = "com.android.settings";
String className = "Settings$AppAndNotificationDashboardActivity";
intent.setClassName(packageName, packageName + "." + className);
activity.startActivity(intent);
So the user can navigate from there, pressing this sequence of menu items:
-> Advanced Parameters ( expandable menu item : not present on tablet, and not sure it's present on phone)
-> Default Apps
-> Default Launcher
This requires 2 or 3 steps that I would like to avoid.
1.2 - This can be improved!
I found out that when the "-> Default Apps" menu item is selected, a (com.android.settings, .SubSettings) Intent (with extra) is launched but I was not able to make this works (permission denial).
But I installed Nova Launcher and it turns out it's able to display the "-> Default Apps" settings page on Huawei devices!
So the user land on a page where she/he only has to tap on "-> Default Launcher" then choose a default launcher: much easier.
2 - Questions:
As I think it's just not possible to display the 'Lancher Picker' on Huawei devices, here is my question:
How can I display the "-> Default Apps" settings page (image down here) on Huawei devices (like Nova Launcher does)?
Are they using another intent action on Huawei devices?
Thanks beforehand your help.
Yes on Huawei devices, Nova uses a different intent to open to the correct screen. I likely found this by using apktool on the Settings.apk pulled from a Huawei device and looking at the AndroidManifest.
Note that "com.android" is always a code smell as it means it's not part of the public API. Also this isn't even really "com.android" as it doesn't exist on AOSP and com.android.settings.PREFERRED_SETTINGS is purely a Huawei invention. It's very likely that some Huawei devices won't have this at all. It's also possible that in the future this intent might continue to work but not do what it currently does. So handle it carefully.
/* Some Huawei devices don't let us reset normally, handle it by opening preferred apps */
Intent preferredApps = new Intent("com.android.settings.PREFERRED_SETTINGS");
preferredApps.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
if (pm.resolveActivity(preferredApps, 0) != null) {
context.startActivity(preferredApps);
} else {
...
}
In fact, the accepted answer is not 100% correct, because it opens a general default apps chooser activity.
It works, but it's better to bring user right to the launcher chooser activity — it's com.google.android.permissioncontroller/com.android.packageinstaller.role.ui.HomeSettingsActivity (at least for the Android 10 Huawei Honors).
So, the correct code snippet is:
Intent()
.apply {
component = ComponentName("com.google.android.permissioncontroller", "com.android.packageinstaller.role.ui.HomeSettingsActivity")
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
}
.takeIf {
packageManager.resolveActivity(it, 0) != null
}
?.let(context::startActivity)
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.
I'm trying to open settings on NFC Tap & Pay page with this piece of code:
startActivity(new Intent(Settings.ACTION_NFC_PAYMENT_SETTINGS));
While testing on LG Nexus 5X with Android 7.1.2 I have received this crash:
android.content.ActivityNotFoundException:
No Activity found to handle Intent { act=android.settings.NFC_PAYMENT_SETTINGS }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1809)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
at android.app.Activity.startActivityForResult(Activity.java:4228)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(SourceFile:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(SourceFile:79)
at android.app.Activity.startActivityForResult(Activity.java:4186)
at android.support.v4.app.FragmentActivity.startActivityForResult(SourceFile:859)
at android.app.Activity.startActivity(Activity.java:4525)
at android.app.Activity.startActivity(Activity.java:4493)
at ...
Well, this crash can be handled easilly with try-catch but what is wierd, when I open this NFC settings manually, code works like a charm - no crash. Why? Does anyone have an explanation for this behavior?
In documentation[1] is written this:
In some cases, a matching Activity may not exist, so ensure you
safeguard against this.
Is it possible that they meant this sentence like "you have to open settings manually, then it works fine"?
[1] https://developer.android.com/reference/android/provider/Settings.html#ACTION_NFC_PAYMENT_SETTINGS
From: https://developer.android.com/reference/android/provider/Settings.html#ACTION_NFC_PAYMENT_SETTINGS
ACTION_NFC_PAYMENT_SETTINGS
added in API level 19
String ACTION_NFC_PAYMENT_SETTINGS
Activity Action:
Show NFC Tap & Pay settings
This shows UI that allows the user to configure Tap&Pay settings.
In some cases, a matching Activity may not exist, so ensure you safeguard against this.
Input: Nothing
Output: Nothing
Constant Value: "android.settings.NFC_PAYMENT_SETTINGS"
ACTION_NFC_PAYMENT_SETTINGS is not supported by your device or can at least not be handled.
Update 1
Since your minAPILevel is 19, the action should be supported by the android RT. However, it is possible, that the link between the action and the NFC settings-menu, ALTHOUGH the menu exists, is not or can not be established.
Try to use Settings.ACTION_NFC_SETTINGS as the action and see if it starts.
If so, I'd expect an implementation issue.
To guard against the exceptions, I'd recommend using:
PackageManager packageManager = getActivity().getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
startActivity(<your intent>);
} else {
Log.d(TAG, "No application available to handle requested action.");
}
See: How to check if an intent can be handled from some activity? for credit and reference.
I have an android application which lists the system and installed apps of a device.Also launch the selected one on click.It works fine.Now i want to disable listing of following applications.
Task Manager(which shows on long pressing home button)
Settings
These are the names used in Samsung Galaxy devices.I want to get the package name and launcher activity name of the application using for these purpose in Sony Ericson,Google phones,HTC and Samsung devices.
Also i want the package name of applications Camera,Email & Gmail using in these devices.
In short : I want to get the package name of these applications in the specified device because there are many apps can be use for a single purpose.For eg : ,There are many apps which can be use as a Task Manager, also we can change the default task manager and use a custom app for this purpose.So i don,t know which is used in users device.We can find the home launcher applications using Intent and ResolverActivity.Like this is there any way to get list of all applications which can be use as task manager,camera handling applications etc...?
Any help is much appreaciating.
Thanks in Advance
see task manager and settings dont have package name you can disable task manager by using this code
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.d("Focus debug", "Focus changed !");
if(!hasFocus) {
Log.d("Focus debug", "Lost focus !");
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
}
}
and settings goto by using
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
By using you can disale and enable. By last gmail,Email,camera you search by net and used in your code.
All the Best:)
Go to google play store and install package name viewer(https://play.google.com/store/apps/details?id=com.gijoon.pkgnameviewer&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5naWpvb24ucGtnbmFtZXZpZXdlciJd) you can see package name for each and every application.