从快捷方式启动活动也始终启动主要活动

Rea*_*lly 5 android android-manifest android-intent android-activity

我已经构建了一个更改用户壁纸的应用程序.我希望添加一个Android快捷方式,这样用户就可以更改自己的壁纸而无需完全打开应用程序(主要用例是将其绑定到Nova Launcher之类的手势,允许您为每个手势选择快捷方式) .

我有一切工作,有一个大问题.每次触发快捷方式时,我的自定义操作都会发生,但主启动活动也会启动!这显然是不可取的,我无法弄清楚发生了什么.

这是ShortcutActivity代码:

public class ShortcutActivity extends Activity {

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

    final Intent intent = getIntent();
    final String action = intent.getAction();

    if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
        setupShortcut();
        finish();
        return;
    } else {
        Toast.makeText(this, "Do something interesting.", Toast.LENGTH_SHORT).show();
        finish();
    }
}

void setupShortcut() {
    Intent shortcutIntent = new Intent("CHANGE");
    shortcutIntent.setClass(this, getClass());

    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_title));
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);

    Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

    setResult(RESULT_OK, intent);
}
}
Run Code Online (Sandbox Code Playgroud)

这是我的(简化)清单:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/title_activity"
    android:theme="@style/AppTheme">
    <activity
        android:name=".WallpaperSettings"
        android:label="@string/title_activity_wallpaper_settings">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".ShortcutActivity"
        android:theme="@android:style/Theme.NoDisplay"
        android:label="@string/shortcut_title">
        <intent-filter>
            <action android:name="android.intent.action.CREATE_SHORTCUT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>
Run Code Online (Sandbox Code Playgroud)

这甚至可以在不触发主发射器活动的情况下完成吗?

Bam*_*amx 6

它也发生在我身上.但是我找到了一个有效的解决方 只需添加android:launchMode="singleInstance"从快捷方式打开的活动即可.

在这种情况下:

<activity
    android:name=".MainActivity2"
    android:label="@string/title_activity_main_activity2" >
    <intent-filter>
        <action android:name="android.intent.action.CREATE_SHORTCUT"/>
        <category android:name="android.intent.category.CUSTOM_TEST" />
    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

我无法找到对此的解释,因为无法真正找到问题的根源.它只是有效

来源:https://stackoverflow.com/a/23002294/1354096


Dro*_*kas 0

我要在这里讲一点“哲学”。因此,当您创建快捷方式时,您想要做的是从主屏幕而不是菜单访问应用程序。但是您想要的是在单击该“快捷方式”时启动另一个活动。这意味着您需要其他活动的启动器图标。正如此处提到的,这没有多大意义。相反,您正在寻找的是一个小部件,它可以根据您的需要打开另一个活动,并且可以放置在屏幕上。为此,Android 开发人员网站有非常详细的说明。

编辑:对于您的问题,我认为您会为这两个活动添加类似的意图过滤器,因此两者都会启动。因为意图过滤器表明它们都是启动器活动。