我构建了一个更改用户壁纸的应用程序。我希望添加一个Android快捷方式,以便用户可以更改壁纸而无需完全打开应用程序(主要用例是将其绑定到Nova Launcher之类的手势,它允许您为每个手势选择一个快捷方式)。
我让一切都在工作,有一个大问题。每次触发快捷方式时,都会发生我的自定义操作,但随后主启动活动也会启动!这显然是不希望的,我无法弄清楚发生了什么。
以下是快捷方式活动代码:
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);
}
}
这是我的(简化)清单:
<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>
这甚至可以在不触发主启动器活动的情况下完成吗?
它也发生在我身上。但是,我找到了一个可行的解决方案。只需将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>
我找不到对此的解释,因为无法真正找到问题的根源。它只是工作
来源: https://stackoverflow.com/a/23002294/1354096
通过执行以下更改来尝试一下。
1)设置快捷方法:
private void setupShortcut() {
Intent shortcutIntent = new Intent(getApplicationContext(),
ShortcutActivity .class);
shortcutIntent.setAction(Intent.ACTION_VIEW);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_title));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher));
addIntent
.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
2) 舱单文件中的声明:
<activity
android:name=".ShortcutActivity"
android:noHistory="true"
android:excludeFromRecents="true"
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>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
我希望它会有所帮助!!
所以我用nova启动器对此进行了测试,它似乎工作正常。
安卓清单.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.geronimo.testapplciation" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<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>
</application>
我学到的东西-类别只是文本,您可以根据自己的意愿进行更改
我可以从从Nova Launcher创建的快捷方式启动MainActivity2
。
如果我从应用程序抽屉启动应用程序,MainActivity1 将打开。
可能的问题:如果从快捷方式打开应用程序,并最小化应用程序,应用程序将从活动 2 恢复。
关键是,您可以像您一样更喜欢您的快捷方式制作机制。如果这是您正在使用的相同方法/代码。好吧,我又被难住了。
我在这里要有点"哲学"。因此,当您创建快捷方式时,您要做的是从主屏幕而不是菜单访问应用程序。但是您想要的是单击该"快捷方式"时启动另一个活动。这意味着您需要一个用于其他活动的启动器图标。正如这里提到的,这没有多大意义。相反,您正在寻找的是一个小部件,它可以根据您的需要打开另一个活动,并且可以放置在屏幕上。为此,Android开发人员网站有一个非常详细的解释。
编辑:至于您的问题,我认为您会为这两个活动添加类似的意图过滤器,因此两者都会启动。因为意图过滤器指示它们都是启动器活动。
你好真实真实随意,
我想您只需要从清单代码中删除类别行。
只需从清单代码中删除:<category android:name="android.intent.category.DEFAULT" />
。
请在此处查看已接受的答案。
希望对您有所帮助。如果它不能解决您的问题,请告诉我。
享受编码... :)