Android桌面快捷方式
2171 点击·0 回帖
![]() | ![]() | |
![]() | Android中的桌面快捷方式和PC机上的快捷方式一样,用于启动某一应用程序。要在桌面添加一个快捷方式非常简单,只需长按桌面或者点击"Menu"按钮,然后在弹出的选项中选择shortcut,然后选择要添加的快捷方式即可。 下面主要介绍如何通过代码将一个应用程序添加到桌面快捷方式。 首先在描述文件AndroidManifest.xml中注册一个action为:<action Android:name="Android.intent.action.CREATE_SHORTCUT"/> 如下所示: [java] <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:Android="http://schemas.Android.com/apk/res/Android" package="com.test.shortcut" Android:versionCode="1" Android:versionName="1.0"> <application Android:icon="@drawable/icon" Android:label="@string/app_name"> <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" /> <action Android:name="Android.intent.action.CREATE_SHORTCUT"/> </intent-filter> </activity> </application> </manifest> 接下来是MainActivity: [java] public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent intent; //判断是否要添加快捷方式 if (this.getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)){ intent = new Intent(); //设置快捷方式名称 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "拨打电话"); //设置快捷方式图标 Parcelable icon = Intent.ShortcutIconResource.fromContext(this, Android.R.drawable.stat_sys_phone_call); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); //设置快捷方式执行的intent Uri uri = Uri.parse("tel:055555"); Intent it = new Intent(Intent.ACTION_DIAL, uri); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, it); setResult(RESULT_OK,intent); }else { //取消 setResult(RESULT_CANCELED);; } this.finish(); } } 代码非常简单,运行程序,长按桌面,选择shortcut后如图所示: ![]() 选择将ShortCut添加到桌面,效果如图: ![]() 单击“拨打电话”图标,出现如图所示结果: ![]() | |
![]() | ![]() |