English 中文(简体)
活动结束后发生的广播火灾
原标题:BroadcastReceiver fires after the activity is already created

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:

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 ";
                }
            }
        }
    }
};

我登记的法典部分:

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);
    }

没有人知道为什么发生这种情况,我会做什么错?

最佳回答

你在你的活动中重新登记你的广播接收器。 这意味着,在你开展这项活动之前,你不能接受广播。

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.

This part is important:

内容本身使属性适用于所有应用部分,包括广播接收器。 The <application>and <receiver> Substances must both be "true” for thecast receiver to be Institution. 如果是“假肢”,则残疾;不能当场。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签