简体   繁体   中英

Turn On GPS like GPS on/off widget

I know that you can't turn on GPS in your application without going to system preferences. But i've downloaded GPS on/off widget and this widget do the job. I've decompiled the apk to find how the programmer have done the job with no success, dex files doen't show the code but functions and informations. Idea on how did he does that ?

Use the following function

buildAlertMessageNoGps() and launchGPSOptions()

to achieve your goal .

see The Link

You can look up the source code if he posted it. Sometimes people host their code on GIT and you can browse their entire project. Specifically unless you're familiar with decompiling code using dalvik.. then that is a tricky route.

If you want to toggle the GPS, just have your widget activate an activity that toggles the GPS via passing the state of the widget to the activity via an intent. Then have your activity parse this and then close itself.

However; I would prefer to do it through a service, which is an activity without a UI. This service can be launched with your app and when a particular broadcast is received, then execute some function, which would be triggered via your widget.

Specifically you'd need to look into one type of broadcast listening. There is the XML based intent filter which will IMMEDIATELY send those particular broadcasts to your app, which can be handled accordingly.

For instance, say your widget is a toggle button with a title. When you toggle the button have you widget code send a broadcast with an intent to your service with the state of the toggle button. Then in the service, you would control the state of the GPS or any other component on the phone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM