标签:
MainActivity
public class MainActivity extends ListActivity {/**安装包所在路径*/private String path = Environment.getExternalStorageDirectory().getAbsolutePath();/**安装包的文件名*/private String fileName = "bqt.apk";private boolean isDeleteShortCut;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);String[] array = { "创建快捷方式,启动应用的入口Activity", "创建快捷方式,启动SecondActivity", //"创建快捷方式,启动ThirdActivity", "创建快捷方式,启动NormalActivity", //"创建快捷方式,通过PackageManager启动", "创建快捷方式,安装指定路径的apk", "=====删除而非创建快捷方式=====" };setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>(Arrays.asList(array))));}@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {Intent intent = new Intent();switch (position) {case 0://可以删除//通过ComponentName启动,第一个参数是包名;第二个是参数类名,要带上包名,貌似只能启动一个应用的入口Activity(即指定)intent.setComponent(new ComponentName("com.bqt.test", "com.bqt.test.MainActivity"));//下面这些参数都是非必须的intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);intent.setAction(Intent.ACTION_MAIN);//决定应用程序最先启动的Activityintent.addCategory(Intent.CATEGORY_LAUNCHER);if (!isDeleteShortCut) createShortCut(this, "MainActivity", R.drawable.ic_launcher, intent);else deleteShortCut(this, "MainActivity", intent);break;case 1://无法删除intent.setComponent(new ComponentName("com.bqt.test", "com.bqt.test.SecondActivity"));if (!isDeleteShortCut) createShortCut(this, "SecondActivity", R.drawable.ic_launcher, intent);else deleteShortCut(this, "SecondActivity", intent);break;case 2://无法删除intent.setComponent(new ComponentName("com.bqt.test", "com.bqt.test.ThirdActivity"));if (!isDeleteShortCut) createShortCut(this, "ThirdActivity", R.drawable.ic_launcher, intent);else deleteShortCut(this, "ThirdActivity", intent);break;case 3://无法删除intent.setComponent(new ComponentName("com.bqt.test", "com.bqt.test.NormalActivity"));if (!isDeleteShortCut) createShortCut(this, "NormalActivity", R.drawable.ic_launcher, intent);else deleteShortCut(this, "NormalActivity", intent);break;case 4://可以删除intent = getPackageManager().getLaunchIntentForPackage("com.bqt.test");intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//非必须if (!isDeleteShortCut) createShortCut(this, "约爱", R.drawable.ic_launcher, intent);else deleteShortCut(this, "约爱", intent);break;case 5:////可以删除。当程序卸载后,【除了】这个图标,其他所有图标都会被删除intent = new Intent(Intent.ACTION_VIEW);intent.setDataAndType(Uri.parse("file://" + path + File.separator + fileName), "application/vnd.android.package-archive");if (!isDeleteShortCut) createShortCut(this, "安装应用", R.drawable.ic_launcher, intent);else deleteShortCut(this, "安装应用", intent);break;case 6:isDeleteShortCut = true;break;}}/*** 删除快捷方式,删除和创建快捷方式的intent需要对应才能找到并成功删除* 权限:com.android.launcher.permission.UNINSTALL_SHORTCUT* */public static void deleteShortCut(Context context, String name, Intent intent) {Intent shortcutIntent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);context.sendBroadcast(shortcutIntent);}/*** 创建快捷方式,实际上是在android的Launcher应用上创建一个图标,系统可能会自动做判断,如果已存在就不再创建快捷方式* 只要intent中的里封装的信息稍有不同,可能就会创建两个不同的快捷方式* 权限:com.android.launcher.permission.INSTALL_SHORTCUT* @param name 快捷方式的名称* @param iconResId 图标id* @param intent 点击后的事件*/public static void createShortCut(Context mContext, String name, int iconResId, Intent intent) {if (isExistShutCut(mContext, name)) return;Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");shortcutintent.putExtra("duplicate", false);// 是否可以重复放置shortcut,定制的系统可能会做限制,不一定有效shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);//名称shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(mContext.getResources(), iconResId));//图标shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);//动作mContext.sendBroadcast(shortcutintent);}/*** 判断快捷图标是否在数据库中已存在,可能获取不到,因为不同手机厂商或第三方的桌面的快捷方式uri都不同;* 提供一个解决办法,创建快捷方式的时候保存到preference,或者建个文件在SD卡上,下次加载的时候判断不存在就先发删除广播,再重新创建* 权限:com.android.launcher.permission.READ_SETTINGS* @param name 快捷方式的名称*/public static boolean isExistShutCut(Context context, String name) {boolean isExist = false;int version = android.os.Build.VERSION.SDK_INT;Uri uri;if (version < 8) uri = Uri.parse("content://com.android.launcher.settings/favorites");else uri = Uri.parse("content://com.android.launcher2.settings/favorites");Cursor c = context.getContentResolver().query(uri, new String[] { "title", "iconResource" }, "title=?", new String[] { name }, null);if (c != null) {if (c.getCount() > 0) isExist = true;c.close();}return isExist;}}
Second
public class SecondActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);TextView textView = new TextView(this);textView.setText("如果存在多个activity都声明了android.intent.action.MAIN和android.intent.category.LAUNCHER\n那么将会有多个图标显示在桌面上\n\n");textView.append("但是只有最前面设置的那个Activity的有效\n\n");textView.append("当点击后面那个Activity对应的图标启动时,若前面的Activity已启动,则跳到前面的那个Activity;除此之外才会启动此对应的Activity!\n\n");textView.append("android.intent.action.MAIN:指定应用程序最先启动的Activity\n\n");textView.append("android.intent.category.LAUNCHER:指定是否显示在Laucher程序列表里(一般就是指桌面应用)\n\n");textView.append("此两者同时存在时,程序列表中才有快捷图标\n\n");setContentView(textView);}}
Third
public class ThirdActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);TextView textView = new TextView(this);textView.setText("仅指定android.intent.action.MAIN,不会显示在Laucher程序列表里,但可以被其他程序直接调起");setContentView(textView);}}
Normal
public class NormalActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);TextView textView = new TextView(this);textView.setText("仅指定android.intent.category.LAUNCHER也是普通的Activity,不可以被其他程序直接调起");setContentView(textView);}}
清单文件
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.bqt.test"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="16"android:targetSdkVersion="21" /><uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name=".MainActivity"android:label="第一个Activity" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name=".SecondActivity"android:label="第二个Activity" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name=".ThirdActivity"android:label="第三个Activity" ><intent-filter><action android:name="android.intent.action.MAIN" /></intent-filter></activity><activityandroid:name=".NormalActivity"android:label="普通的Activity" ><intent-filter><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>
标签:
原文地址:http://www.cnblogs.com/baiqiantao/p/5613355.html