如何强制使用zxing lib只用我的应用程序?

时间:2012-07-19 11:09:29

标签: android zxing

好吧,假设有3个不同的应用程序在手机上使用zxing lib。每当我想用我自己的应用程序打开zxing时,android会问我是否使用app 1或app 2或我自己的应用程序完成操作。如何强制它仅通过我的应用程序运行而没有任何对话框?有没有机会这样做?

修改

除了CommonsWare之外,如果你想在上面处理条形码结果,你可以这样做 其他活动。

步骤1:跳转到Capture Activity中名为handleDecode的方法。在handleDecodeInternally(rawResult,resultHandler,barcode);

之后添加这些行
Intent intent = new Intent(getIntent().getAction());
intent.putExtra("SCAN_RESULT", rawResult.getText());
setResult(RESULT_OK,intent);
finish();

第2步:对其他活动的onActivityResult事件做任何想做的事情。

PS:再次感谢CommonsWare。

4 个答案:

答案 0 :(得分:7)

首先,没有“zxing lib”。您应该使用Barcode Scanner应用程序,在活动级别将其绑定到您的应用程序中,最好使用他们的IntentIntegrator代码。 Here is a sample application demonstrating this。 ZXing的创建者特别不支持或认可将条形码扫描器源代码烘焙到另一个应用程序中。

但是,鉴于您的症状,我必须假设您正在尝试将条形码扫描器源代码添加到您自己的应用程序中。

您可能在扫描活动元素的清单中有类似的内容:

        <intent-filter >
            <action android:name="com.google.zxing.client.android.SCAN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

您不是条形码扫描仪。然而,这<intent-filter>声称您是条形码扫描仪。

您需要删除此<intent-filter>,修改条形码扫描程序源代码的副本以使其不需要,然后使用基于组件的Intent构造函数启动扫描活动(例如{ {1}})。

答案 1 :(得分:4)

只是包含这个,这已经为我做了所需..

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.setPackage(getPackageName());
            intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
            startActivityForResult(intent, 0);

答案 2 :(得分:0)

Android不允许您自行设置。只有用户才能为操作设置默认应用程序。如果在手机上,您希望自己的应用处理该事件,请在选择器中选择应用之前选中“使用为默认值”框。

出于安全原因,Android不允许您在没有用户交互的情况下将您的应用设置为默认应用,因为恶意应用可能会将自己绑定为各种事件的默认设置。

答案 3 :(得分:0)

实际上你需要删除像CommonsWare所说的intent-filter,所以必须如下:

<activity
       android:name="com.google.zxing.client.android.CaptureActivity"
       android:screenOrientation="landscape"
       android:configChanges="orientation|keyboardHidden"
       android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
       android:windowSoftInputMode="stateAlwaysHidden">
</activity>

而不是通过外部意图调用你应该像zxing一样调用zxing:

private final static int ACTION_ZXING_SCANNER = 0x0000c0de; //IntentIntegrator.REQUEST_CODE
private void startZxingScanner() {
    final Intent intent = new Intent(this, com.google.zxing.client.android.CaptureActivity.class);
    intent.setAction(Intents.Scan.ACTION);
    startActivityForResult(intent, ACTION_ZXING_SCANNER);
}

然后使用请求代码onActivityResult()处理ACTION_ZXING_SCANNER中的结果。如果需要,导入字符串:

import com.google.zxing.client.android.Intents;

注意:这对我有用,我将zxing项目作为lib添加到我的项目中,所以这里是 - “zxing lib”:)