安装在android中时,无法在主屏幕上创建应用程序的快捷方式(Unable to create application's shortcut on Home Screen upon ins

编程入门 行业动态 更新时间:2024-10-25 01:22:44
安装在android中时,无法在主屏幕上创建应用程序的快捷方式(Unable to create application's shortcut on Home Screen upon installation in android)

我想在安装时在home screen上创建应用程序的快捷方式。

我将此添加到AndroidManifest.xml

<activity android:name="com.example.test.ShortCutActivity" android:icon="@drawable/ic_launcher" android:label="@string/title_activity_short_cut" > <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>

此外,我在项目中使用ShortCutActivity名称创建了Actvity。

ShortCutActivity.java

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ShortcutIconResource icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher); Intent intent = new Intent(); Intent launchIntent = new Intent(this,MainActivity.class); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortCut"); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); setResult(RESULT_OK, intent); }

但它并没有创造任何捷径。 我错过了什么吗?

I want to create a shortcut of the application on home screen when it is installed.

I added this to AndroidManifest.xml

<activity android:name="com.example.test.ShortCutActivity" android:icon="@drawable/ic_launcher" android:label="@string/title_activity_short_cut" > <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>

Also I have a Actvity created with ShortCutActivity name in my project.

ShortCutActivity.java

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ShortcutIconResource icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher); Intent intent = new Intent(); Intent launchIntent = new Intent(this,MainActivity.class); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortCut"); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); setResult(RESULT_OK, intent); }

But it is not creating any shortcut. Am I missing something ?

最满意答案

在您的活动中使用此示例代码,该代码在AndroidManifest.xml中设置为LAUNCHER和MAIN

private void createShortcut(){ Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class); shortcutIntent.setAction(Intent.ACTION_MAIN); Intent intent = new Intent(); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortcutDemo"); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher)); intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); getApplicationContext().sendBroadcast(intent); }

同时在AndroidManifest.xml中应用以下权限

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

希望这对你有用

Use this sample code in your activity which is set as a LAUNCHER and MAIN in AndroidManifest.xml

private void createShortcut(){ Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class); shortcutIntent.setAction(Intent.ACTION_MAIN); Intent intent = new Intent(); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortcutDemo"); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher)); intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); getApplicationContext().sendBroadcast(intent); }

Also apply following permissions in your AndroidManifest.xml

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

Hope this will work for you

更多推荐

intent,android,ShortCutActivity,icon,电脑培训,计算机培训,IT培训"/> <meta nam

本文发布于:2023-07-29 18:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1318621.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:快捷方式   中时   应用程序   屏幕上   安装在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!