android 添加桌面快捷方式
、在桌面创建快捷方式方法:
方法一:通过长按某一个应用程序的图标在桌面上创建启动该应用程序的快捷方式。
这个方法安装完程序都用户都能实现。
方法二:在应用程序中构建一个Intent,然后以Broadcast的形式通知Launcher创建快捷方式。
先看Launcher的AndroidMainfest.xml文件中InstallShortcutReceiver的注册信息:
- <!--设置wallpapaer的activity -->
- <!-- Intent received used to install shortcuts from other applications -->
- <receiver
- android:name="com.android.launcher2.InstallShortcutReceiver"
- android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
- <intent-filter>
- <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
- </intent-filter>
- </receiver>
- <!--设置wallpapaer的activity -->
- <!-- Intent received used to install shortcuts from other applications -->
- <receiver
- android:name="com.android.launcher2.InstallShortcutReceiver"
- android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
- <intent-filter>
- <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
- </intent-filter>
- </receiver>
所以向这个BroadcastReceiver发送广播,首先应用程序必须要 有com.android.launcher.permission.INSTALL_SHORTCUT权限,然后广播去的Intent的action设置为
com.android.launcher.action.INSTALL_SHORTCUT。
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.studio.android.ch10.ex1"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/ji" android:label="@string/app_name">
- <activity android:name=".UrgentCall"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="3" />
- <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
- </manifest>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.studio.android.ch10.ex1"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/ji" android:label="@string/app_name">
- <activity android:name=".UrgentCall"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="3" />
- <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
- </manifest>
- import android.app.Activity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Parcelable;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class UrgentCall extends Activity implements
- OnClickListener {
- Button police;
- Button fire;
- Intent directCall;
- private final String ACTION_ADD_SHORTCUT =
- "com.android.launcher.action.INSTALL_SHORTCUT";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- police = (Button)findViewById(R.id.police);
- fire = (Button)findViewById(R.id.firepolice);
- police.setOnClickListener(this);
- fire.setOnClickListener(this);
- directCall = new Intent(Intent.ACTION_CALL);
- }
- @Override
- public void onClick(View v) {
- Intent addShortcut =
- new Intent(ACTION_ADD_SHORTCUT);
- String numToDial = null;
- Parcelable icon = null;
- switch (v.getId()) {
- case R.id.police:
- numToDial = "110";
- icon = Intent.ShortcutIconResource.fromContext(
- this,R.drawable.jing);
- break;
- case R.id.firepolice:
- numToDial = "119";
- icon = Intent.ShortcutIconResource.fromContext(
- this,R.drawable.huo);
- break;
- default:
- break;
- }
- addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
- numToDial);
- directCall.setData(Uri.parse("tel://"+numToDial));
- addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
- directCall);
- addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
- icon);
- sendBroadcast(addShortcut);
- }
- }
- import android.app.Activity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Parcelable;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class UrgentCall extends Activity implements
- OnClickListener {
- Button police;
- Button fire;
- Intent directCall;
- private final String ACTION_ADD_SHORTCUT =
- "com.android.launcher.action.INSTALL_SHORTCUT";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- police = (Button)findViewById(R.id.police);
- fire = (Button)findViewById(R.id.firepolice);
- police.setOnClickListener(this);
- fire.setOnClickListener(this);
- directCall = new Intent(Intent.ACTION_CALL);
- }
- @Override
- public void onClick(View v) {
- Intent addShortcut =
- new Intent(ACTION_ADD_SHORTCUT);
- String numToDial = null;
- Parcelable icon = null;
- switch (v.getId()) {
- case R.id.police:
- numToDial = "110";
- icon = Intent.ShortcutIconResource.fromContext(
- this,R.drawable.jing);
- break;
- case R.id.firepolice:
- numToDial = "119";
- icon = Intent.ShortcutIconResource.fromContext(
- this,R.drawable.huo);
- break;
- default:
- break;
- }
- addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
- numToDial);
- directCall.setData(Uri.parse("tel://"+numToDial));
- addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
- directCall);
- addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
- icon);
- sendBroadcast(addShortcut);
- }
- }
方法三:为应用程序组件注册一个符合特定条件的IntentFilter,然后就可以直接在Launcher的桌面上添加启动该组件的快捷方式了。
当我们在Home应用程序Launcher的桌面空白处长按触摸时,会出现一个对话框,提示选择要添加的桌面组件。当我们想把添加的快捷方式的Activity添加进这列时,只需要在这个Activity注册时添加一个Action为android.intent.action.CREATE_SHORTCUT的IntentFilter就可以。
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.studio.android.ch10.ex1"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/ji" android:label="@string/app_name">
- <activity android:name=".UrgentCall"
- 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" />
- </intent-filter>
- </activity>
- <activity android:name=".FireShortcut">
- <intent-filter>
- <action android:name="android.intent.action.CREATE_SHORTCUT" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="3" />
- <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
- </manifest>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.studio.android.ch10.ex1"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/ji" android:label="@string/app_name">
- <activity android:name=".UrgentCall"
- 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" />
- </intent-filter>
- </activity>
- <activity android:name=".FireShortcut">
- <intent-filter>
- <action android:name="android.intent.action.CREATE_SHORTCUT" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="3" />
- <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
- </manifest>
- import android.app.Activity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Parcelable;
- public class FireShortcut extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Intent addShortcut;
- //若是“添加快捷方式”的Action就初始化快捷方式的Intent
- if (getIntent().getAction()
- .equals(Intent.ACTION_CREATE_SHORTCUT)) {
- /*初始化添加快捷图标的Intent*/
- addShortcut = new Intent();
- addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
- "119");
- Parcelable icon = Intent.ShortcutIconResource.fromContext(
- this,R.drawable.huo);
- addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
- icon);
- Intent callFirePolice =
- new Intent(Intent.ACTION_CALL,Uri.parse("tel://119"));
- addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
- callFirePolice);
- /*设置Result*/
- //因为Action是由Launcher通过startActivityForResult这个方法发出的。
- setResult(RESULT_OK,addShortcut);
- } else {
- setResult(RESULT_CANCELED);
- }
- finish();
- }
- }
- import android.app.Activity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Parcelable;
- public class FireShortcut extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Intent addShortcut;
- //若是“添加快捷方式”的Action就初始化快捷方式的Intent
- if (getIntent().getAction()
- .equals(Intent.ACTION_CREATE_SHORTCUT)) {
- /*初始化添加快捷图标的Intent*/
- addShortcut = new Intent();
- addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
- "119");
- Parcelable icon = Intent.ShortcutIconResource.fromContext(
- this,R.drawable.huo);
- addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
- icon);
- Intent callFirePolice =
- new Intent(Intent.ACTION_CALL,Uri.parse("tel://119"));
- addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
- callFirePolice);
- /*设置Result*/
- //因为Action是由Launcher通过startActivityForResult这个方法发出的。
- setResult(RESULT_OK,addShortcut);
- } else {
- setResult(RESULT_CANCELED);
- }
- finish();
- }
- }
这时列表中会有两个UrgentCall的选项,第二个就直接在桌面添加“拨打火警119”的快捷方式了。
- UrgentCall.rar (35.8 KB)
android 添加桌面快捷方式的更多相关文章
- Android 添加桌面快捷方式操作
/** * 为程序创建桌面快捷方式 */ private void addShortcut(){ Intent shortcut = new Intent(“com.android.launcher. ...
- Android添加桌面快捷方式的简单实现
核心代码如下: Button bn = (Button) findViewById(R.id.bn); // 为按钮的单击事件添加监听器 bn.setOnClickListener(new OnCli ...
- kailli添加桌面快捷方式
kailli添加桌面快捷方式 /usr/share/applications/xxx.desktop 注意大小写要与Name对应 [Desktop Entry] Version=1.0 Name=Tu ...
- Android -- 创建桌面快捷方式
代码 /** * * 返回添加到桌 ...
- (转)Android创建桌面快捷方式两种方法
[IT168技术]Android在桌面上生成快捷方式有两种情况,一种是直接在桌面直接生成;一种是长按桌面,在弹出的快捷菜单中生成. 谈谈在桌面上直接生成.个人觉得这个比较爽快,既然都是快捷方式了干嘛还 ...
- Android创建桌面快捷方式
在桌面上创建特定界面的快捷入口,icon和title根据请求参数命名.在网上收集的一些相关资 料,在使用intent发送广播的时候,一些型号的收集会有问题,如魅族MX,红米,以及华为,使用setCla ...
- Android 通过程序添加桌面快捷方式
原理:通过代码向 Launcher 中的广播接收者发送广播来创建快捷图标. 首先要声明的权限是: <!--添加图标的权限--> <uses-permission android:na ...
- Android中为你的应用程序添加桌面快捷方式
public void ShortCut(View view){ createDeskShortCut(this,getString(R.string.short_cut),R.drawable.up ...
- android添加桌面悬浮窗
1. 添加权限 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 2. ...
随机推荐
- tyvj P2018 「Nescafé26」小猫爬山 解题报告
P2018 「Nescafé26」小猫爬山 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 Freda和rainbow饲养了N只小猫,这天,小猫们要去爬山.经 ...
- Web系统自己主动化部署脚本
Web开发的项目,除了在本地直接执行外,还可能常常须要在server上部署. 写了个自己主动化部署的脚本,仅供參考. 不少地方须要配置路径.个人建议使用绝对路径,不用依赖执行脚本时所在的路径. #!/ ...
- java web中servlet、jsp、html 互相访问的路径问题
java web中servlet.jsp.html 互相访问的路径问题 在java web种经常出现 404找不到网页的错误,究其原因,一般是访问的路径不对. java web中的路径使用按我的分法可 ...
- libcurl使用easy模式阻塞卡死等问题的完美解决---超时设置
libcurl使用时疑难问题: 在使用libcurl时, jwisp发现, curl_easy_perform是阻塞的方式进行下载的, curl_easy_perform执行后,程序会在这里阻塞等待下 ...
- linux中sort命令
功能说明:将文本文件内容加以排序,sort可针对文本文件的内容,以行为单位来排序. 参 数: -b 忽略每行前面开始出的空格字符. -c 检查文件是否已经按照顺序排序. -d 排序时,处理英文字母.数 ...
- 关于NLPIR应用在KETTLE中的探索
一:什么是NLPIR? NLPIR汉语分词系统(自然语言处理与信息检索共享平台),主要功能包括中文分词:词性标注:命名实体识别:用户词典功能:支持GBK编码.UTF8编码.BIG5编码.新增微博分词. ...
- 设置网站expires和max-age属性
转:http://www.zicheng.net/article/982022.htm 在使用百度站长工具测试网站优化建议时,在 设置静态内容缓存时间 栏目里,会提示 类似 FAILED - (未设置 ...
- IStat Menus 5.02 5.03 的注册码
1574-5977-7956-8062-0000 6015-5448-3282-4975-0000 9665-5955-6856-2071-0000 2447-9517-7939-5221-0000
- 在Foreda上安装apache-tomcat-7.0.42.tar.gz
开发环境JDK和Tomcat应该和部署环境一致,要不容易出现奇奇怪怪的问题.所以Aspire机器上的Tomcat要装一个新版本了. 装Tomcat基本等于一个解压和移动的过程,确实简单. 第一步:解压 ...
- sqoop安装部署(笔记)
sqoop是一个把关系型数据库数据抽向hadoop的工具.同时,也支持将hive.pig等查询的结果导入关系型数据库中存储.由于,笔者部署的hadoop版本是2.2.0,所以sqoop的版本是:sqo ...