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. ...
随机推荐
- 使用supervisor过程的坑
1.安装:由于使用的是公司的虚拟机,所以使用pip install supervisor的过程遇到很多权限问题. 中间尝试使用sudo pip install supervisor的方式安装,但是使用 ...
- 使用Scala
1. 净资产应用实例 我们要构建这样一个应用,它会取回一份列表,其中包括用户持有的股票的代码以及股份,并告知他们在当前日期为止的这些投资的总价.这包含了几件事:获取用户输入.读文件.解析数据.写文件. ...
- OBjective-C:在可变数组NSMutableArray中添加相同对象后,进行自定义的排序方式输出
以下为自定义的排序方式的实现 #import "Person+Compare.h" @implementation Person (Compare) -(NSComparisonR ...
- Informatica 常用组件Aggregator之一 聚合表达式
转换类型:已连接.主动 聚合转换允许您执行聚合计算,比如平均值和总和.聚合转换与表达式转换不同,您可以使用聚合转换对多组执行计算.而表达式转换只允许您逐行地执行计算. 使用 ...
- 基于zabbix的Redis、Sentinel、Slave多实例自动发现监控
约定 保证whereis redis-cli 能够正确返回redis-cli程序的路径 保证 redis的配置文件在模板宏{$REDIS_SERVER_CONFIG_PATH}的路径,并且后缀名 为. ...
- tensorflow 之常见模块conv,bn...实现
使用tensorflow时,会发现tf.nn,tf.layers, tf.contrib模块有很多功能是重复的,尤其是卷积操作,在使用的时候,我们可以根据需要现在不同的模块.但有些时候可以一起混用. ...
- Reverse Linked List II leetcode java
题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1 ...
- 你需要知道的、有用的 Python 功能和特点
在使用Python多年以后,我偶然发现了一些我们过去不知道的功能和特性.一些可以说是非常有用,但却没有充分利用.考虑到这一点,我编辑了一些的你应该了解的Pyghon功能特色. 带任意数量参数的函数 你 ...
- Android生成带图片的二维码
一.问题描述 在开发中需要将信息转换为二维码存储并要求带有公司的logo,我们知道Google的Zxing开源项目就很好的帮助我们实现条形码.二维码的生成和解析,但带有logo的官网并没有提供demo ...
- golang struct转map
struct转map package main import ( "fmt" "reflect" "time" ) type User st ...