Android Scenario Mode Development (i)-control gps/wifi/Bluetooth/flight mode

Source: Internet
Author: User

For the control of GPs on Android, the official provides the relevant API


Settings.Secure.setLocationProviderEnabled (Getcontentresolver (), Locationmanager.gps_provider, true);

But when we call the Setlocationproviderenabled method, the system throws an exception prompt that requires Android.permission.WRITE_SECURE_SETTINGS permission, even if The same is true for requests that add this permission in Mainfest. Setlocationproviderenabled This method requires root permissions, in order for this code to execute, the program must be the system's app, that is, installed under/system/app, or request root permissions, so that in addition to the manufacturer can install their own app Under System/app, you can only request root permission.

Baidu a bit, there are solutions, but said not too clear, and directly copy their code, after the operation of the GPS state has not changed. And it doesn't explain its principle.

In fact, in addition to using Settings.Secure.setLocationProviderEnabled this method, there is another way is to use the system's own power control widget to change the status of GPs.

The specific code is as follows:

Intent Intent = new Intent ();
Intent.setclassname ("Com.android.settings", "Com.android.settings.widget.SettingsAppWidgetProvider");
Intent.addcategory ("Android.intent.category.ALTERNATIVE");
Intent.setdata (Uri.parse ("Custom:3"));
Context.sendbroadcast (Intent);

What does this piece of code mean? In fact, through the intent message sent to Com.android.settings.widget.SettingsAppWidgetProvider to deal with, because this is the system comes with the program, so he has root privileges.

That uri.parse ("Custom:3") and what does that mean, in fact Custom:3 is the power control plug-in corresponding to the various buttons, by viewing the code can know the situation of each ID

Private StaticFinalintButton_bluetooth = 4;
PrivateStaticFinalintbutton_brightness = 1;
PrivateStaticFinalintButton_gps = 3;
PrivateStaticFinal intButton_sync = 2;
PrivateStaticFinalintButton_wifi = 0;

This makes it possible to change the GPS status by sending a message to Com.android.settings.widget.SettingsAppWidgetProvider. Also, changing the state of other system settings can be done in this way.

Next control the WiFi switch is very easy, as long as the simple call code can be implemented

Wifimanager manager =null;
Manager = (Wifimanager) context.getsystemservice (Context.wifi_service);
Manager.setwifienabled (false);
Manager.setwifienabled (true);

Switch to control Bluetooth

Bluetoothadapter bluetoothadapter = Bluetoothadapter.getdefaultadapter ();
Bluetoothadapter.disable ();
Bluetoothadapter.enable ();

Control flight mode, Google does not provide the relevant API, but we can through the intent broadcast to achieve

Intent Intent;
Settings.System.putInt (Context.getcontentresolver (), Settings.System.AIRPLANE_MODE_ON, enabled 1:0);
Intent = New Intent (intent.action_airplane_mode_changed);
Intent.putextra ("state", enabled);
Context.sendbroadcast (Intent);

In this way, we have completed the function of controlling gps/wifi/Bluetooth/flight mode in the situational mode.


Android Scenario Mode Development (i)-control gps/wifi/Bluetooth/flight mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.