设立广播接收器打开蓝牙 [英] setting up broadcast Receiver to turn on bluetooth

查看:243
本文介绍了设立广播接收器打开蓝牙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java和Android的新手,我试图找出如何做以下动作。顺便说一句,我发现谷歌官方Android文档是不是新手,想任何转诊的东西多一点...非本地程序员友好。

好吧,这里有云:

我想自动开启/关闭蓝牙,当我插上/拔下手机 - 电脑或AC

所以,我已经找到了以下组件:

BatteryManager:好像用 ACTION_BATTERY_CHANGED 的意图是要走的路,并建立一个广播接收机此更改时。难道是正确的吗?

我还发现BluetoothAdapter及内,有使能()方法。它说:没有明确的用户操作不使用打开蓝牙。 - 在手机中插入一个明确的用户操作?这是我用正确的事情?这同样适用于禁止(),当然。

另外,我在互联网(S)看到的例子为广播接收器唯一告诉我如何启动一个意图 - 通常是打开一个新的活动。我以为我可以有广播接收器发射中,我将打开蓝牙和关闭的方法?意图可能是一种方法,而不仅仅是活动?

任何帮助将是AP preciated!


解决方案

  

BatteryManager:好像用ACTION_BATTERY_CHANGED目的是要走的路,并建立一个广播接收器时,这个变化。难道是正确的吗?


是的,这是监督的事情,当充电器已连接(包括堵漏与充电能力的USB端口)喜欢的一种方式。您还可以使用 ACTION_POWER_CONNECTED


  

在手机中插入一个明确的用户操作?


没有 - 绝对不是,除非你创建一个弹出窗口询问用户的同意。引用的文档的使能()方法...


  

蓝牙不应该没有直接的用户同意的情况下启用。如果您想以创建无线连接开启蓝牙,您应该使用ACTION_REQUEST_ENABLE意图,这将提高一个对话框请求用户权限才能打开蓝牙即可。仅对于包括一个用户接口,用于改变系统设置,例如功率管理器的应用程序的应用程序所提供的启用()方法


当我插上我的设备充电,9次了10年,我不希望它做什么,但收费。如果我已经安装了我的手机上你的应用程序,并发现它被悄悄启用蓝牙,没有我的知识,你的应用程序将是未安装的直线距离。

有已知蓝牙,根据用户设置,你可以把别人的设备处于危险的安全问题,如果是不知情的情况下启用。


  

意图可以是一个方法,而不仅仅是活动?


没有,一个意图是一个消息系统的一部分。请参见意图和意图过滤器。是的,我知道这是不是很'新手友好,但Android是一个复杂的野兽,如果你真的想为它成功的程序,你一定要读这样的东西。

I'm a newbie in both Java and Android and I'm trying to figure out how to do the following action. Btw, I find that the official Google Android documentation is NOT for newbies and would like any referral to something a bit more... non-native programmer friendly.

Ok, here goes:

I would like to turn on/off Bluetooth automatically when I plug in/unplug the phone - pc or ac.

So, I have found the following components:

BatteryManager: Seems like using the ACTION_BATTERY_CHANGED intent is the way to go, and setting up a broadcast receiver for when this changes. Is it correct?

I've also found BluetoothAdapter and within that, there's the enable() method. Which says: "do not use without explicit user action to turn on Bluetooth." - is plugging in the phone a explicit user action? Is this the right thing for me to use? The same goes for disable(), of course.

Also, the BroadcastReceivers I've seen examples for in the Internet(s) only show me how to launch an intent - usually to open a new activity. I assume I can have the Broadcast Receiver launch a method in which I will turn bluetooth on and off? Can an intent be a method, and not just activity?

Any help would be appreciated!

解决方案

BatteryManager: Seems like using the ACTION_BATTERY_CHANGED intent is the way to go, and setting up a broadcast receiver for when this changes. Is it correct?

Yes, that is one way to monitor things like when a charger has been connected (including plugging to a USB port with charge capability). You can also use ACTION_POWER_CONNECTED.

is plugging in the phone a explicit user action?

NO - most definitely not unless you create a pop-up asking for the user's consent. To quote the docs for the enable() method...

Bluetooth should never be enabled without direct user consent. If you want to turn on Bluetooth in order to create a wireless connection, you should use the ACTION_REQUEST_ENABLE Intent, which will raise a dialog that requests user permission to turn on Bluetooth. The enable() method is provided only for applications that include a user interface for changing system settings, such as a "power manager" app.

When I plug my device to charge, 9 times out of 10 I don't want it to do anything but charge. If I had your app installed on my phone and found out it was enabling Bluetooth silently and without my knowledge, your app would be un-installed straight away.

There are known security issues with Bluetooth and, depending on user settings, you could put somebody's device at risk if it is enabled without their knowledge.

Can an intent be a method, and not just activity?

No, an Intent is part of a messaging system. See Intents and Intent Filters. Yes, I know it's not very 'newbie-friendly' but Android is a complex beast and if you really want to program successfully for it you've got to read this sort of stuff.

这篇关于设立广播接收器打开蓝牙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆