Android启动应用程序从QR码与params

时间:2012-04-21 11:51:17

标签: android qr-code

我想知道在Android中是否可以使用QR Code阅读器启动应用程序。我想要实现的目标是:

  

我创建QR码并在用QR码阅读器扫描后,我需要用一些参数启动我的应用程序,也许它看起来像这样:myApp://org.hardartcore.myApp?myParams或者类似的东西,不太确定。

无论如何都要实现这一目标并获得构建在qr代码中的param,以便启动应用程序。

3 个答案:

答案 0 :(得分:10)

使用以下文字创建QR CODE:myApp://extraString并使用任何qr代码阅读器阅读。或者甚至可以使用Zxing的开源集成您自己的qr代码阅读器。您可以使用extraString获取getIntent().getDataString()作为@Sean Owen。并且不要忘记在清单文件中添加它:

<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="myApp"/>

这应该有效。

答案 1 :(得分:3)

是。在AndroidManifest.xml中,在<activity>中,声明应用响应此网址:

  <intent-filter>
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <data android:scheme="myApp" android:host="org.hardartcore.myApp" android:path="/"/>
  </intent-filter>

(我认为你可能必须在“?”之前以“/”结束才能使用。)

然后,使用该平台解析网址的任何内容都会打开您的应用。 Web浏览器中的超链接可以正常工作。

可以使用getIntent().getDataString()检索网址本身。您可以根据需要将其解析为android.net.Uri

请查看ZXing中的CaptureActivity,了解其执行方式的示例。

答案 2 :(得分:-1)

是可以通过使用意图。

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

public void onActivityResult(int requestCode, int resultCode, Intent intent) 
{
if (requestCode == 0) 
{
if (resultCode == RESULT_OK) 
     {
              final String contents = intent.getStringExtra("SCAN_RESULT");
        Toast.makeText(this, contents, Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) 
        {
    Toast.makeText(this, "Not proper QRCODE...!",Toast.LENGTH_SHORT).show();
    }
}
}