我的自定义意图不起作用。似乎古怪
My Custom Intents do not work. Seems flakey
我正在尝试让一个 activity 通过 intent 和 intent 过滤器启动另一个。这是我的。
在发射中activity:
Intent i = new Intent();
i.setAction("com.test.apps.CATAPP");
i.addCategory("com.test.apps");
this.startActivityForResult(i,APP_REQUEST_CODE);
接收应用程序中的意图过滤器:
<intent-filter>
<action android:name="com.test.apps.CATAPP" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.test.apps" />
<data android:mimeType="text/plain"/>
</intent-filter>
这不起作用。但是,如果我以 Wei Meng Lee 的 Android 4 Development 中的示例为例,那么只有当 URI 和 Intent.ACTION_VIEW 存在时,它才有效。
启动应用中的代码:
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
i.setAction("com.test.apps.CATAPP");
i.addCategory("com.test.apps");
this.startActivityForResult(i,APP_REQUEST_CODE);
打算在接收应用中:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="com.test.apps.CATAPP" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.test.apps" />
<data android:scheme="http"/>
</intent-filter>
我哪里错了?我犯了一个简单的错误吗?
我花了最后几个小时拖网堆栈溢出,以回答这个问题,但只发现了类似的问题,答案没有解决我的问题或不适用。
在您的第一个示例中,您的 Intent 不会与 Activity 的 IntentFilter 匹配。您已经在 IntentFilter 中指定了一个数据元素,但您还没有在 Intent 上设置一个。您可以在 Intent 上设置 MIME 类型:
i.setType("text/plain");
或从清单中的 <intent-filter>
中删除 <data>
元素,具体取决于您的要求。
如果你只是想传递一些简单的文本,如上所示设置 Intent 的类型,并附加一个额外的字符串。
i.putExtra(Intent.EXTRA_TEXT, "Plain text.");
然后,检索下一个Activity中的文本:
String plainText = getIntent().getStringExtra(Intent.EXTRA_TEXT);
if(plainText != null)
...
请注意,Intent.EXTRA_TEXT
常量只是为了方便。额外的密钥可以是任何你想要的,只要它在两个地方是一样的。
我正在尝试让一个 activity 通过 intent 和 intent 过滤器启动另一个。这是我的。
在发射中activity:
Intent i = new Intent();
i.setAction("com.test.apps.CATAPP");
i.addCategory("com.test.apps");
this.startActivityForResult(i,APP_REQUEST_CODE);
接收应用程序中的意图过滤器:
<intent-filter>
<action android:name="com.test.apps.CATAPP" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.test.apps" />
<data android:mimeType="text/plain"/>
</intent-filter>
这不起作用。但是,如果我以 Wei Meng Lee 的 Android 4 Development 中的示例为例,那么只有当 URI 和 Intent.ACTION_VIEW 存在时,它才有效。
启动应用中的代码:
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
i.setAction("com.test.apps.CATAPP");
i.addCategory("com.test.apps");
this.startActivityForResult(i,APP_REQUEST_CODE);
打算在接收应用中:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="com.test.apps.CATAPP" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.test.apps" />
<data android:scheme="http"/>
</intent-filter>
我哪里错了?我犯了一个简单的错误吗?
我花了最后几个小时拖网堆栈溢出,以回答这个问题,但只发现了类似的问题,答案没有解决我的问题或不适用。
在您的第一个示例中,您的 Intent 不会与 Activity 的 IntentFilter 匹配。您已经在 IntentFilter 中指定了一个数据元素,但您还没有在 Intent 上设置一个。您可以在 Intent 上设置 MIME 类型:
i.setType("text/plain");
或从清单中的 <intent-filter>
中删除 <data>
元素,具体取决于您的要求。
如果你只是想传递一些简单的文本,如上所示设置 Intent 的类型,并附加一个额外的字符串。
i.putExtra(Intent.EXTRA_TEXT, "Plain text.");
然后,检索下一个Activity中的文本:
String plainText = getIntent().getStringExtra(Intent.EXTRA_TEXT);
if(plainText != null)
...
请注意,Intent.EXTRA_TEXT
常量只是为了方便。额外的密钥可以是任何你想要的,只要它在两个地方是一样的。