后已创建活动的BroadcastReceiver火灾 [英] BroadcastReceiver fires after the activity is already created
问题描述
我想用一个BroadcastReceiver获得许可与USB设备进行通信。我想实现它是在Android网站 HTTP做了同样的方式:// developer.android.com/guide/topics/usb/host.html
这一切的作品,那种。
但广播接收器是在创建的主要活动后才fireing。我这就意味着能够与设备进行通信仅关闭后,应用程序,然后再次打开它(当我不注销的BroadcastReceiver,当我这样做,我不能在所有通信)。
可能是什么原因?
我的code是这样的:
I want to use a BroadcastReceiver to get permission to communicate with a USB device. I am trying to implement it the same way it is done on android website http://developer.android.com/guide/topics/usb/host.html It all works, kind of. But the broadcastReceiver is fireing only after the main activity is created. Which means I am able to communicate with the device only after close the app and open it again (when I don't unregister the broadcastReceiver, when I do I can't communicate at all). What can be the reason? My code is like this:
私人最终的BroadcastReceiver mUsbReceiver =新的广播接收器()
{
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action))
{
synchronized (this)
{
device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false))
{
if(device != null)
{
//things I do when the permission is granted
}
}
else
{
devMessage = "permission denied for device ";
}
}
}
}
};
在code,其中我注册它的一部分:
The part of the code where I register it:
公共无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while(deviceIterator.hasNext())
{
device = deviceIterator.next();
mUsbManager.requestPermission(device, mPermissionIntent);
}
// ...
if(device!=null)
{
// ...
}
else
{
// ...
}
tv.setText(devMessage);
}
有谁知道为什么会这样,我可能是做错了?
Does anyone know why is this happening, what I might be doing wrong?
推荐答案
您要注册的广播接收器在你的活动。这意味着,您在运行活动之前,你不能接收广播。
You're registering your broadcast receiver in you activity. That means that before you run that activity, you cannot receive broadcasts.
您或许应该看看你的AndroidManifest.xml中注册reciever标签。
这是接收器标签中的文档。这可以让你无需启动你的活动注册接收器。
You should probably look at registering a reciever-tag in in you AndroidManifest.xml. This is the docs for the receiver-tag. This allows you to register receivers without starting your activity.
这部分是很重要的:
在&lt;用途&gt;元素适用于所有应用程序组件,包括广播接收器自身的启用属性。在&lt;用途&gt;和&lt;接收器GT;属性必须要启用的广播接收机都为真。如果任何一个为假,它被禁用;它不能被实例化。
The <application> element has its own enabled attribute that applies to all application components, including broadcast receivers. The <application> and <receiver> attributes must both be "true" for the broadcast receiver to be enabled. If either is "false", it is disabled; it cannot be instantiated.
这篇关于后已创建活动的BroadcastReceiver火灾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!