通过意向在Android中创建快捷方式 [英] Creating shortcuts in Android via Intent
本文介绍了通过意向在Android中创建快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
<一href="http://stackoverflow.com/questions/6337431/android-create-shortcuts-on-the-home-screen">Android建立在主屏幕上的快捷方式
我有一个的TextView
和在我的活动(HomeActivity.class)按钮
目标:当我点击按钮
就应在进入创建默认的Android形象的图标和文字的快捷方式的TextView
。
Goal: When I click on the Button
it should create a shortcut with the default android image as icon and text as entered in the TextView
.
到目前为止,我发现,我们可以创建一个使用 <快捷方式code> ACTION_CREATE_SHORTCUT
So far I've found out that we can create shortcuts using ACTION_CREATE_SHORTCUT
这是在code我到目前为止有:
This is the code I have so far:
Intent result = new Intent(android.content.Intent.ACTION_CREATE_SHORTCUT);
result.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
new Intent(getApplicationContext(), Editor.class));
result.putExtra(TodoDbAdapter.KEY_ROWID,rowid);
result.putExtra(Intent.EXTRA_SHORTCUT_NAME,textView.getText());
result.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(HomeActivity.this,
R.drawable.icon));
setResult(RESULT_OK, result);
我的清单文件:
My Manifest File :
<activity android:name=".HomeActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
但是,这code不会创建任何快捷方式。
But this code does not create any shortcut.
我如何可以创建快捷方式?
How can I create shortcuts?
推荐答案
试试这个:
Intent shortcutIntent;
shortcutIntent = new Intent();
shortcutIntent.setComponent(new ComponentName(activity.getPackageName(), ".classname"));
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final Intent putShortCutIntent = new Intent();
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"Title");
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(PersonProfile.this, R.drawable.icon));
putShortCutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(putShortCutIntent);
和清单中添加此权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
这篇关于通过意向在Android中创建快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文