我有这个应用程序:
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 中将 Intent 定义为:
<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 捕获器仅挂起并显示屏幕“AuthTestLabel”。
MainActivity 似乎根本没有被执行。 我做错了什么?
我创建了一个空白的 Android 项目(没有任何界面),因为该应用程序应该只计算一次性密码并将其显示在屏幕上的对话框中(或将其放入剪贴板),具体取决于该应用程序是否是从单击的链接调用的或扫描的二维码。 (这就是为什么我定义 2 个 urlhandlers sqrsa 和 cqrsa)
应用程序有什么问题?
最佳答案
您正在 if 语句中将字符串与 ==
运算符进行比较,在这种情况下可能会返回 false
您需要将字符串与 equals()
进行比较,例如将 data.getScheme() == "sqrsa"
更改为 (data.getScheme() .equals("sqrsa"))
关于java - 为什么我的应用程序无法处理我的 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22214899/