使用隐式意图调用主活动(指定动作和类别)



我正试图使用隐式意图调用主活动。我给出了动作和类别的意图,但在开始活动之前,android系统会给我一个打开活动的应用程序列表。

我用来调用主要活动的代码片段如下:

protected void initiateActivity(int requestCode, String value, String oper) {
        Intent i = new Intent("android.intent.action.MAIN");
        i.addCategory("android.intent.category.LAUNCHER");
        i.putExtra("VALUE", value);
        i.putExtra("OPER", oper);
        startActivityForResult(i, requestCode);
    }

在我看来,系统中的每个应用程序都会有相同的动作、类别组合,因此android会给我一个可供选择的应用程序列表。我可以对我的"主要活动"进行哪些更改以避免出现此问题?

看起来您可能需要使用意向过滤器进行分离。看起来这篇文章有一个很好的解释:

如何在<意图过滤器>?

它建议添加下面这样的过滤器,否则你将调用启动器:

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:windowSoftInputMode="stateHidden"
          android:screenOrientation="portrait">
    <intent-filter>
          <action android:name="android.intent.action.MAIN"/>
          <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
          <action android:name="com.package.name.MyAction"/>
          <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

最新更新