[Android开发实战]Android添加快捷方式到桌面的两种方式

来源:互联网 发布:网络运营部职责 编辑:程序博客网 时间:2024/04/28 05:45

转载请注明出处:http://blog.csdn.net/ruils/article/details/16923201


最近研究了一下金山清理大师一键加速快捷方式动画的实现(原文),顺便也总结下Android中如何添加快捷方式到桌面。

方式一:

我称之为被动的Action方式。

在高版本的android系统(android4.3,4.4)中,从Lanucher的程序列表小部件中,长按某个小部件来添加,在低版本的android系统中,长按桌面,会出现添加快捷方式的选项。

为了能在小部件中或者快捷方式的选项目中出现我们自己的程序,

首完要在AndroidManifest.xml中申明一个能响应"android.intent.action.CREATE_SHORTCUT"的Activity:

例如:

  <activity            android:name="com.cleanmanager.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.CREATE_SHORTCUT" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>        </activity>


其次,在响应这个Acitivity中设置快捷方式的属性:

             Intent shortcutIntent = new Intent();//设置点击快捷方式时启动的Activity,因为是从Lanucher中启动,所以包名类名要写全。shortcutIntent.setComponent(new ComponentName(getPackageName(),getPackageName() + "."+ AnimationActivity.class.getSimpleName()));//设置启动的模式shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS| Intent.FLAG_ACTIVITY_NEW_TASK);Intent resultIntent = new Intent();//设置快捷方式图标resultIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this,R.drawable.shortcut_proc_clean));//启动的IntentresultIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);//设置快捷方式的名称resultIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));                setResult(RESULT_OK, resultIntent);        finish();


最后,一定要记得setResult(RESULT_OK, resultIntent); 为什么称之为被动的Action模式呢,原因就在这里,Lanucher找我要快捷方式,我就给它,怎么给的呢,就是这个setResult(RESULT_OK, resultIntent);


方式二:

我称之为主动的发广播方式。

首先,既然是发广播,发给谁?当然是发给Lanucher, Lanucher要不要呢,那么我申明一个权限,给它老板打声招呼,它不要也得要。老板是谁?Android系统是也。

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

其次,就是在发广播的Acitivity中设置快捷方式的属性:

                Intent shortcutIntent = new Intent();//设置点击快捷方式时启动的Activity,因为是从Lanucher中启动,所以包名类名要写全。shortcutIntent.setComponent(new ComponentName(getPackageName(),getPackageName() + "."+ AnimationActivity.class.getSimpleName()));//设置启动的模式shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS| Intent.FLAG_ACTIVITY_NEW_TASK);Intent resultIntent = new Intent();//设置快捷方式图标resultIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this,R.drawable.shortcut_proc_clean));//启动的IntentresultIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);//设置快捷方式的名称resultIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));                                resultIntent.setAction(ACTION_INSTALL_SHORTCUT);                sendBroadcast(resultIntent);

ACTION_INSTALL_SHORTCUT是

public static final String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";


最后就把快捷方式发给Lanucher了,这种方式是主动的,在我们的代码逻辑中随时可以通过广播的方式发送快捷方式给Lanucher。


想知道这两种方式的实现原理,请看Lanucher代码!

第一种方式的实现原理请参考:

packages/apps/Launcher2/src/com/android/launcher2/Launcher.java  1875行

第二种方式的实现原理请参考:

packages/apps/Launcher2/src/com/android/launcher2/InstallShortcutReceiver.java


最后,如果您喜欢本文章,请点赞!

示例源码下载:http://download.csdn.net/detail/u012379847/6604299

我把这两种方式都写在MainActivity中了,请注意查看

         // 长按方式添加快捷方式----->即被动的Action方式。if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {Log.d(TAG, "action " + action);setResult(RESULT_OK, resultIntent);finish();} else {// 发送广播方式添加快捷方式----->即主动的发广播方式Log.d(TAG, "sendBroadcast " + resultIntent);resultIntent.setAction(ACTION_INSTALL_SHORTCUT);sendBroadcast(resultIntent);}
原创粉丝点击