将setContentView(R.layout.main)移动到Broadcast接收器类

时间:2014-04-27 12:13:24

标签: java android android-layout android-intent

我有mUsbReceiverBroadcastReceiver)和CameraActivitysetContentView(R.layout.main)来自CameraActivity的接收者Intent。然后CamearActivity使用此值更新其View。请注意,setContentView位于Broadcast接收器类中,而不是CameraActivity类。

public class CameraActivity extends Activity {

private static final String TAG = "openXC::Activity";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    usbConnection();

}

public void usbConnection() {
    UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);


   PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_CANCEL_CURRENT);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
   registerReceiver(mUsbReceiver, filter);
    String txt = "default";
    HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
    Log.i(TAG, "Device List: " + deviceList);
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    UsbDevice device = deviceIterator.next();
    Log.i(TAG, "Device List: " + deviceList);
    mUsbManager.requestPermission(device, mPermissionIntent);
}

private static final String ACTION_USB_PERMISSION ="com.ford.openxc.webcam.USB_PERMISSION";
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) {
                UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    if(device != null){
                        Log.d(TAG, "Displayed Comten View " + device);
                        setContentView(R.layout.main);

                   }
                } 
                else {
                    Log.d(TAG, "permission denied for device " + device);
                }
            }
        }
    }
 };
}

这有时会很好,但有时会引发以下错误

I/openXC::Activity( 5609): Device List: {/dev/bus/usb/001/002=UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1133,mProductId=2085,mClass=239,mSubclass=2,mProtocol=1,mInterfaces=[Landroid.os.Parcelable;@421a1f50]}
I/openXC::Activity( 5609): Device List: {/dev/bus/usb/001/002=UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1133,mProductId=2085,mClass=239,mSubclass=2,mProtocol=1,mInterfaces=[Landroid.os.Parcelable;@421a1f50]}
I/Adreno200-EGLSUB( 5609): <ConfigWindowMatch:2087>: Format RGBA_8888.
E/        ( 5609): <s3dReadConfigFile:75>: Can't open file for reading
E/        ( 5609): <s3dReadConfigFile:75>: Can't open file for reading
D/openXC::Activity( 5609): Displayed Comten View UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=1133,mProductId=2085,mClass=239,mSubclass=2,mProtocol=1,mInterfaces=[Landroid.os.Parcelable;@421d3ed0]
D/WebcamPreview( 5609): WebcamPreview constructed

2 个答案:

答案 0 :(得分:0)

从技术上讲,只要在事件线程上执行,就可以调用setContentView。

否则你需要使用Handler来调用它。

此外,以下是一些可能对您有用的有用链接:

link1

link 2

link 3

答案 1 :(得分:0)

我没有太多的关于USB的东西,但因为你说它的说法不能读取文件..我相信dat错误可能在usb中所以为了调试目的我建议移动setContentView(int)来自if直接调用onRecieve的条件,所以每当调用onReceive时,你的contenview都会改变,这将有助于确保错误不在setcontentview中......在没有setcontentview的情况下你可以看到usb,现在如果错误即将来临肯定是错误在usb而不是在setContentView ....

希望它有效:)