Android(安卓)创建快捷方式
16lz
2021-01-25
快捷方式提供一种快速访问目标应用的方式。在Android开发中,同样可以从代码上创建和删除快捷方式。下面做简要说明如何处理。先看效果图『创建-创建效果-删除-删除效果』:
添加删除快捷方式:
出现在这里的方法是在Manifest文件设定的,具体如下:
添加删除快捷方式:
import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.Intent.ShortcutIconResource;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class AActivity extends Activity implements OnClickListener {private Button addButton;private Button deleteButton;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);addButton = (Button) findViewById(R.id.button1);addButton.setOnClickListener(this);deleteButton = (Button) findViewById(R.id.button2);deleteButton.setOnClickListener(this);}private void addShortcut() {// 安装的IntentIntent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");// 快捷名称shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));shortcut.putExtra("duplicate", false);// 启动对象ComponentName comp = new ComponentName(this.getPackageName(), "." + this.getLocalClassName());shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));// 快捷图标ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);// 发送广播sendBroadcast(shortcut);}private void deleteShortcut() {// 卸载的IntentIntent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");// 快捷名称shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));// 指定对象String appClass = this.getPackageName() + "." + this.getLocalClassName();ComponentName comp = new ComponentName(this.getPackageName(), appClass);shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));// 发送广播sendBroadcast(shortcut);}@Overridepublic void onClick(View v) {if (v == addButton) {addShortcut();return;}if (v == deleteButton) {deleteShortcut();return;}}}说明:
- 构建ComponentName时,注意“.”的使用,否则会找不到需要启动的目标;
- 布局文件比较简单,可以在附件中找到;
- 如果希望可以重复创建,只要设置shortcut.putExtra("duplicate", true);就OK了;
Manifest文件权限设置:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />另一个效果:
出现在这里的方法是在Manifest文件设定的,具体如下:
<activity-alias android:label="Shortcut" android:name=".CreateShortcuts" android:targetActivity=".BActivity" > <intent-filter > <action android:name="android.intent.action.CREATE_SHORTCUT" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity-alias>说明:关于具体说明,还是看开发者网站吧,那里比较详细!『偷懒了』:) 这种方式实现的方式还是和上一种类似,只不过把快捷方式选择添加到了系统快捷方式页面罢了!还是需要在targetActivity中做一些操作才能创建快捷方式。下面看看BActivity做了什么吧:
import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Parcelable;public class BActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent();String action = intent.getAction();if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {shortcut();finish();return;}setContentView(R.layout.activity_b); } void shortcut() {Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);shortcutIntent.setClass(this, this.getClass());Intent intent = new Intent();intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷图标");intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);Parcelable shortIcon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortIcon);setResult(RESULT_OK, intent);} }看到了吧,这里还是在代码中创建快捷方式的!:) 多说一句:一些问题的解决,与其钻牛角尖,不如借鉴源码来的更快!:)
更多相关文章
- GitHub 标星 2.5K+!教你通过玩游戏的方式学习 VIM!
- 一款常用的 Squid 日志分析工具
- GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
- RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
- Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
- android 扩散水波纹效果
- Android: /cache中的文件是怎么消失的[转]
- Android(安卓)Studio 出现Error:Connection timed out: connect.
- Android之——AIDL小结