使用自定义操作启动活动

Jas*_*sby 40 android android-intent android-activity

我希望使用自定义操作在我的应用中启动活动.我找到了一些答案,但我尝试的所有内容都会java.lang.RuntimeException说没有找到任何活动来处理Intent {act = com.example.foo.bar.YOUR_ACTION}.

这是我的清单文件中的活动:

<activity
    android:name=".FeedbackActivity" >  
    <intent-filter>
        <action android:name="com.example.foo.bar.YOUR_ACTION" />
    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

这就是我开始活动的方式:

Intent intent = new Intent("com.example.foo.bar.YOUR_ACTION");
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.

Mak*_*aks 115

我认为你需要的是为你的intent-filter添加一个默认类别,例如.

<activity
    android:name=".FeedbackActivity" >  
    <intent-filter>
        <action android:name="com.example.foo.bar.YOUR_ACTION" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅此答案.

  • 这是正确答案,应标记为正确答案. (6认同)

Foa*_*Guy 28

我认为你的意图是错误的.试试这样:

String CUSTOM_ACTION = "com.example.foo.bar.YOUR_ACTION";

//Intent i = new Intent(this, FeedBackActivity.class);  // <--- You might need to do it this way.
Intent i = new Intent();
i.setAction(CUSTOM_ACTION);

startActivity(i);
Run Code Online (Sandbox Code Playgroud)

  • 他正在做[很好](http://developer.android.com/reference/android/content/Intent.html#Intent(java.lang.String)).他刚刚错过了DEFAULT类别. (12认同)
  • 调用setAction()与调用新的Intent(String)构造函数相同 (2认同)