How do i popup a message box that tells the user to switch on their GPS settings. The popup message should also contain a button that would take the user directly to their settings. How can i do it.
I will leave creation of the dialog / layout / anything else which will display the prompt.
Opening localization settings:
Intent viewIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(viewIntent);
Detecting localization enabled:
How to check if Location Services are enabled?
Related
I am using the background services and it does not run on some devices as I have to enable the autostart permission for the app. I want to enable the autostart permission automatically where it needs. I am using this code but it redirects me on the window where the autostart permission is.
Note :-- What I want , Not to redirect the user on the settings screen.
Here is my code for xiaomi
if (manufacturer.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
//this will open auto start screen where user can enable permission for your app
Intent intent1 = new Intent();
intent1.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
startActivity(intent1);
}
Thanks !
I am building an android application where I am using some services. My services get close when application is close in some custom android OS like MI.
Then I figure out we have to push our application in white list of security autostart application.
I got some SOF link's where they are suggesting below answer.
String manufacturer = "xiaomi";
if(manufacturer.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
//this will open auto start screen where user can enable permission for your app
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
startActivity(intent);
}
This above code used to send user to that particular page.
Now I have to check if my application is disable so I can send user to this page and if it is enable then move to other screen. Is there any way to check this?
There is no Android API to check if the the AutoStart is enabled or not. Though you can have your own logic in the App (may be you can use preference to set a Boolean for it) to check this. Also the above way to enabling the AutoStart might not work always. Please have a look here
If we use this code we see a message: searching for GPS. However, the GPS symbol is merely shown; GPS doesn't actually work:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
this.sendBroadcast(intent);
}
Why isn't it working ? And how to make it work correctly ?
That's not allowed anymore. If you take a look at this bug report, this hack was subverted in Android 4.4. It still works on older OS versions, though you shouldn't be using it anywhere now. To quote that report:
This doesn't work ... however, it can cause stuff to react as if the GPS status changed -- for example, the HTC One S will show the GPS icon in the status bar, even though the GPS is still disabled.
That explains why you can see the GPS icon even though it isn't actually ON. Now as for why you can't do that ...
Android's GPS technology periodically sends location data to Google even when no third-party apps are actually using the GPS function. In many Western countries this is seen as a major violation of privacy. That's why Google made it mandatory to get the user's consent before using the GPS function. The following dialog is seen whenever the user turns GPS on:
And hence it is no longer possible to programmatically change the GPS settings, as by necessity it requires the user's permission. What the programmer can do is direct the user to the GPS settings by calling
startActivity(context, new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
and let the user make a choice.
As an interesting point, if you try sending the GPS_ENABLED_CHANGE broadcast on the new OS versions, you get a
java.lang.SecurityException: Permission Denial:
not allowed to send broadcast android.location.GPS_ENABLED_CHANGE
error. As you can see, its a SecurityException with a permission denial message.
Is there any way to disable google location service programmatically?
I want to write an app to turn it off with a widget.
Thanks for your help!
Is there any way to disable google location service programmatically?
No, sorry, you cannot disable GPS, etc. programmatically, except via an app installed on the system partition or signed by the firmware's signing key. IOW, you need a rooted phone.
As far as i know, the LocationManager is a system service that can not be turned off. Though, you can disable all the location providers that the service uses, such as Wireless location, GPS, etc.
If you're using the UiAutomater, you can turn off Location Services by simulating a click on the Location option in the Quick Settings. The filter logic may be platform dependent. You may analyse the view hierarchy using the uiautomatorviewer.
i.e.
UiDevice.getInstance(getInstrumentation()).openQuickSettings();
UiDevice.getInstance(getInstrumentation()).findObject(new UiSelector().descriptionContains("Location").className(ViewGroup.class)).click();
build.gradle
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
Short answer is, No. But if you really need to turn it off, you can prompt the user to do it:
LocationManager loc = (LocationManager) this.getSystemService( Context.LOCATION_SERVICE );
if( loc.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER ) )
{
Toast.makeText( this, "Please turn off GPS", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS );
startActivity(myIntent);
}
else
{
// Do nothing
}
This might not help your situation but is the only way I know of changing the settings.
I am fetching Latitude and Longitude through network
It is giving me latitude and longitude but what if my NETWORK LOCATION PROVIDER is unchecked
It will never provide me the Latitude and Longitude of the current location.
What i want is, How Can I enable the NETWORK LOCATION PROVIDER in phone without knowing
user that It is opened,
How can i do that?
What i have tried is:
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 1);
But this will open the page in my phone to check it i want to check AUTOMATICALLY
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.phone", "com.android.phone.Settings");
startActivity(intent);
It is impossible to enable the network provider location automatically as already mentioned. The correct way is to display a box and redirect the user to the settings page. You can enable GPS by using a slight hack as mentioned here. Still I wouldn't suggest doing that.
Not possible to enable it automatically. Network settings must be enabled manually by user only. You can show a message to the user to enable his network.
You can able to open the Location Services page to enable it. By programmatic, not possible.