我试图创建应用程序快捷方式,我参考了一些文档,并且我正在使用
Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
launchIntent.setAction(Intent.ACTION_MAIN);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplication(), MainActivity.class));
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "test app");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.mipmap.ic_launcher));
getApplicationContext().sendBroadcast(intent);
我还添加了我的清单
的权限<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
然而,它在我的一个旧的HTC手机(android 4.4)上工作正常,但在另一部手机samsung s6(android7.0)上没有。
任何人都可以帮助我吗? THX!
答案 0 :(得分:0)
您应该尝试引入新的运行时权限模型。根据此模型,在安装应用程序时不会提示用户获得权限,而是开发人员需要在运行时检查并请求权限
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.INSTALL_SHORTCUT) != PackageManager.PERMISSION_GRANTED) {
if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.INSTALL_SHORTCUT)) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Need Permission");
builder.setMessage("This app needpermission.");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.INSTALL_SHORTCUT}, INSTALL_SHORTCUT);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
您也可以使用该代码:〜
public static void addShortcut(Context context)
{
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
ApplicationInfo appInfo = context.getApplicationInfo();
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "appInfo.name");
shortcut.putExtra("duplicate", false);
ComponentName component = new ComponentName(appInfo.packageName, "MainActivity.class");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(component));
ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(context, appInfo.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
context.sendBroadcast(shortcut);
}
答案 1 :(得分:0)
使用方法减少混淆。尝试这样做会有效
public void createShortCut(){
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), EnterActivity.class));
sendBroadcast(shortcutintent);
}
答案 2 :(得分:0)
你错误的是你应该将@PostMapping(value = "/api_url")
public ResponseEntity<Object> suggestBreakfast(@RequestBody Map bothVar){
System.out.println("value1 is : "+ bothVar.get("value1"));
System.out.println("value2 is : "+ bothVar.get("value2"));
}
// And i am getting those two values here successfully
提供给EXTRA_SHORTCUT_INTENT意图。
您可以使用以下方法创建或删除Shotcuts
Intent.setAction(Intent.ACTION_MAIN);
确保您的ShortCutActivity在mainfest文件中有private void createOrRemoveShortcut(String name, boolean create) {
Intent shortcutIntent = new Intent(getActivity(), ShortCutActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
if (create) {
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getActivity(),
R.mipmap.ic_launcher));
addIntent
.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate
} else {
addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
}
getActivity().sendBroadcast(addIntent);
}
android.intent.action.CREATE_SHORTCUT
如果要删除快捷方式,则应在mainfest中添加以下权限,
<activity
android:name=".ui.activity.ShortCutActivity"
android:excludeFromRecents="true"
android:label="@string/title_activity_sample"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
答案 3 :(得分:0)
最后我让它发挥作用,我不确定以下哪一个成功,但我改变的是这一行
new Intent(getApplication(), MainActivity.class));
我在其上添加其他类别和操作
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setAction(Intent.ACTION_MAIN);
它正在发挥作用。