布局代码:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" > 5 6 <Button 7 android:layout_width="wrap_content" 8 android:layout_height="wrap_content" 9 android:onClick="create" 10 android:text="创建快捷图标" /> 11 12 <Button 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:onClick="delete" 16 android:text="删除快捷图标" /> 17 18 </LinearLayout>
清单文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.android.hzy.shortcut" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="8" /> 10 <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/> 11 <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/> 12 <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/> 13 14 <application 15 android:allowBackup="true" 16 android:icon="@drawable/ic_launcher" 17 android:label="@string/app_name" 18 android:theme="@style/AppTheme" > 19 <activity 20 android:name="com.android.hzy.shortcut.MainActivity" 21 android:label="@string/app_name" > 22 <intent-filter> 23 <action android:name="android.intent.action.MAIN" /> 24 25 <category android:name="android.intent.category.LAUNCHER" /> 26 </intent-filter> 27 </activity> 28 </application> 29 30 </manifest>
MainActivity
package com.android.hzy.shortcut; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.os.Parcelable; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void create(View v){ if(isExist()){ // 如果存在 土司提示用户 Toast.makeText(this, "快捷方式已经存在", 0).show(); }else{ Intent intent = new Intent(); intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式"); // 快捷方式的名字 // 指定快捷方式的图标 Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon); // 用的是应用默认的图标 // Intent.EXTRA_SHORTCUT_ICON_RESOURCE 可以指定图标 // 点击快捷应用后要启动的activity Intent i = new Intent(); // 启动快捷图标的activity必须是主程序的入口 i.setAction(Intent.ACTION_MAIN); i.addCategory(Intent.CATEGORY_LAUNCHER); // 决定应用程序是否显示在程序列表里 i.setComponent(new ComponentName(this, MainActivity.class)); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, i); sendBroadcast(intent); } } // 判断快捷方式是否在桌面 需要用到provider private boolean isExist(){ boolean isExist = false; Uri uri = null; if(getSDKVersion() < 8){ // 2.2版本之前是没有2的 uri = Uri.parse("content://com.android.launcher.settings/favorites"); // favorites /data/data/launcher里面的数据库 }else{ uri = Uri.parse("content://com.android.launcher2.settings/favorites"); } String selection = " title = ? "; String[] selectionArgs = new String[]{"快捷方式"}; Cursor cursor = getContentResolver().query(uri, null, selection, selectionArgs, null); if(cursor.moveToNext()){ isExist = true; } cursor.close(); return isExist; } /** * 得到当前手机SDK的版本 * @return */ private int getSDKVersion(){ return android.os.Build.VERSION.SDK_INT; } public void delete(View v){ Intent intent = new Intent(); intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT"); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式"); Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon); // 激活的组件 Intent i = new Intent(); i.setAction(Intent.ACTION_MAIN); i.addCategory(Intent.CATEGORY_LAUNCHER); i.setComponent(new ComponentName(this, MainActivity.class)); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, i); sendBroadcast(intent); } }