【问题标题】:Why wont my app process my intent?为什么我的应用程序不会处理我的意图?
【发布时间】:2014-03-06 04:24:10
【问题描述】:

我有这个应用程序:

import android.R.drawable;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;

public class MainActivity extends Activity {

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


        String b64encoded = null;
        Intent intent = getIntent();
        Uri data = intent.getData();
        b64encoded = data.getEncodedSchemeSpecificPart();
        if (b64encoded != null) {

            if (data.getScheme() == "cqrsa") {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Authentication");
                builder.setMessage("Clicked: " + b64encoded);
                builder.setCancelable(false);
                builder.setIcon(drawable.ic_lock_lock);
                builder.setNeutralButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();
            }
            if (data.getScheme() == "sqrsa") {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Authentication");
                builder.setMessage("Scanned: " + b64encoded);
                builder.setCancelable(false);
                builder.setIcon(drawable.ic_lock_lock);
                builder.setNeutralButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();
            }
        }
        else
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Authentication");
            builder.setMessage("No data was supplied");
            builder.setCancelable(false);
            builder.setIcon(drawable.ic_dialog_alert);
            builder.setNeutralButton(android.R.string.ok,
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                    android.os.Process.killProcess(android.os.Process.myPid());
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }    

    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}

然后我将 AndroidManifest 中的意图定义为:

<activity android:name="eu.sebbe.www.qrsaauthentication.MainActivity" android:label="AuthTestLabel">
        <intent-filter>
             <data android:scheme="cqrsa" />
             <data android:scheme="sqrsa" />                        
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>
</activity>

但是,在测试应用程序时,Intent catcher 只会挂起并显示屏幕“AuthTestLabel”。

似乎 MainActivity 根本没有执行。 我做错了什么?

我创建了一个空白的 Android 项目(没有任何界面),因为这个应用程序应该只计算一个一次性密码并将其显示在一个对话框的屏幕上(或将它放在剪贴板中),这取决于应用程序是否是从点击的链接调用的或扫描的二维码。 (这就是为什么我定义了 2 个 urlhandlers,sqrsa 和 cqrsa)

应用程序有什么问题?

【问题讨论】:

标签: java android android-intent url-scheme main-activity


【解决方案1】:

您在 if 语句中将字符串与 == 运算符进行比较,在这种情况下可能会返回 false

您需要将字符串与equals() 进行比较,例如将data.getScheme() == "sqrsa" 更改为(data.getScheme().equals ("sqrsa"))

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多