Android scenario development-control GPS/WiFi/Bluetooth/flight mode/control mute/volume/Vibration

Source: Internet
Author: User

Http://www.cnblogs.com/wii/archive/2012/03/18/2404947.html

Official APIs are provided for GPS control on Android.

Settings. Secure. setlocationproviderenabled (getcontentresolver (), locationmanager. gps_provider, true );
 
However, when we call the setlocationproviderenabled method, the system throws an exception and prompts that the permission for Android. Permission. write_secure_settings is required, even if the permission is added to mainfest. The setlocationproviderenabled method requires the root permission. to execute this code, the program must be a system app, that is, installed under/system/APP; or the root permission is requested, in this way, apart from the vendor's ability to install its own app under/system/APP, the vendor only needs to request the root permission.
 
Baidu has a solution, but it is not clear, and they copy their code directly. After running, the GPS status has not changed. It does not explain its principles.
In addition to setting. Secure. setlocationproviderenabled, you can use the power control widget that comes with the system to change the GPS status.
The 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 code mean? In fact, the message is sent to com. Android. settings. widget. settingsappwidgetprovider through intent for processing. Because this is a built-in program of the system, he has the root permission.
What does URI. parse ("custom: 3") mean? In fact, custom: 3 is the corresponding buttons on the power control plug-in. You can view and change the code to know the status of each ID.
 
Private Static final int button_bluetooth = 4;
Private Static final int button_brightness = 1;
Private Static final int button_gps = 3;
Private Static final int button_sync = 2;
Private Static final int button_wifi = 0;
 
In this way, you can change the GPS status by sending a message to com. Android. settings. widget. settingsappwidgetprovider. You can also change the status of other system settings through this method.
 
 
Next, it's easy to control the Wi-Fi switch. You just need to call the code to implement it.
 
Wifimanager manager = NULL;
Manager = (wifimanager) Context. getsystemservice (context. wifi_service );
Manager. setwifienabled (false );
Manager. setwifienabled (true );
 
 
Bluetooth control switch
Descrithadapter = descrithadapter. getdefaadapter adapter ();
Descrithadapter. Disable ();
Optional thadapter. Enable ();
 
Google does not provide related APIs for controlling the flight mode, but we can implement it through intent broadcast.
 
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, the GPS, Wi-Fi, Bluetooth, and flight modes are controlled in the scene mode.

Http://www.cnblogs.com/wii/archive/2012/03/18/2404947.html in the previous article
We can now control the GPS, Wi-Fi, Bluetooth, and flight modes.
Let's control the volume and vibration of the system.
 
The SDK provides a class to control the volume and vibration of the system. Android. Media. audiomanager uses this class.
Set the phone to mute or vibrate, and change the vibration type. Audiomanager cannot be instantiated in code. You need to obtain its instance through context. getsystemservice (context. audio_service. OK. After obtaining the audiomanager instance, we can call the audiomanager method setringermode to control the sound type. The setringermode method accepts a parameter. You only need to input the parameter based on the Type value to be changed. These three are:
 
 
Public static final int ringer_mode_silent = 0;
Public static final int ringer_mode_vibrate = 1;
Public static final int ringer_mode_normal = 2;
 
 
The following code changes the voice to mute.
 
 
 
Audio = (audiomanager) Context. getsystemservice (context. audio_service );
Audio. setringermode (audiomanager. ringer_mode_silent );
 
 
After mute is set to normal, you can call the code:
 
Audio. setringermode (audiomanager. ringer_mode_normal );
Audio. setstreamvolume (audiomanager. ringer_mode_normal, ringervolume, 0 );
 
 
The second parameter is the volume of the ringtone.
 
OK. After the sound is controlled, let's control the vibration.
 
There are several types of vibration in the system, which are always and never. Only four types of vibration are available.
 
The vibration control is completed by the audiomanager instance. The setvibratesetting method is called to set the mobile phone vibration.
 
For the setvibratesetting method, two parameters are required. The first parameter is the vibration type, vibrate_type_notification and vibrate_type_ringer.
 
The second is vibrate_setting_on, vibrate_setting_off, vibrate_setting_only_silent, vibrate_setting_on,
 
According to the parameter name, we all know the specific function. I will not explain it.
 
Next, let's implement several types of vibration provided in the system.
 
 

Int vibrate_setting =-1;
Int ring_mode =-1;
Switch (profile. vibrate ){
Case profileconstants. vibrate_always_on: // always vibrate
Vibrate_setting = audiomanager. vibrate_setting_on;
Ring_mode = 1;
Break;
Case profileconstants. vibrate_never: // no vibration
Vibrate_setting = audiomanager. vibrate_setting_off;
Ring_mode = 0;
Break;
Case profileconstants. vibrate_only_in_silent: // mute Vibration
Vibrate_setting = audiomanager. vibrate_setting_only_silent;
Ring_mode = 1;
Break;
Case profileconstants. vibrate_unless_silent: // non-Mute Vibration
Vibrate_setting = audiomanager. vibrate_setting_on;
Ring_mode = 0;
Break;
}
Settings. system. putint (Resolver, "vibrate_in_silent", ring_mode );
Audio. setvibratesetting (audiomanager. vibrate_type_ringer, vibrate_setting );

 

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.