android 添加桌面快捷方式

、在桌面创建快捷方式方法:

方法一:通过长按某一个应用程序的图标在桌面上创建启动该应用程序的快捷方式。

这个方法安装完程序都用户都能实现。

方法二:在应用程序中构建一个Intent,然后以Broadcast的形式通知Launcher创建快捷方式。

先看Launcher的AndroidMainfest.xml文件中InstallShortcutReceiver的注册信息:

Xml代码  

  1. <!--设置wallpapaer的activity -->
  2. <!-- Intent received used to install shortcuts from other applications -->
  3. <receiver
  4. android:name="com.android.launcher2.InstallShortcutReceiver"
  5. android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
  6. <intent-filter>
  7. <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
  8. </intent-filter>
  9. </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代码  

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.studio.android.ch10.ex1"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <application android:icon="@drawable/ji" android:label="@string/app_name">
  7. <activity android:name=".UrgentCall"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. </application>
  15. <uses-sdk android:minSdkVersion="3" />
  16. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
  17. </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> 

Java代码  

  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.os.Bundle;
  5. import android.os.Parcelable;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. public class UrgentCall extends Activity implements
  10. OnClickListener {
  11. Button police;
  12. Button fire;
  13. Intent directCall;
  14. private final String ACTION_ADD_SHORTCUT =
  15. "com.android.launcher.action.INSTALL_SHORTCUT";
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. police = (Button)findViewById(R.id.police);
  21. fire = (Button)findViewById(R.id.firepolice);
  22. police.setOnClickListener(this);
  23. fire.setOnClickListener(this);
  24. directCall = new Intent(Intent.ACTION_CALL);
  25. }
  26. @Override
  27. public void onClick(View v) {
  28. Intent addShortcut =
  29. new Intent(ACTION_ADD_SHORTCUT);
  30. String numToDial = null;
  31. Parcelable icon = null;
  32. switch (v.getId()) {
  33. case R.id.police:
  34. numToDial = "110";
  35. icon = Intent.ShortcutIconResource.fromContext(
  36. this,R.drawable.jing);
  37. break;
  38. case R.id.firepolice:
  39. numToDial = "119";
  40. icon = Intent.ShortcutIconResource.fromContext(
  41. this,R.drawable.huo);
  42. break;
  43. default:
  44. break;
  45. }
  46. addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
  47. numToDial);
  48. directCall.setData(Uri.parse("tel://"+numToDial));
  49. addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
  50. directCall);
  51. addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
  52. icon);
  53. sendBroadcast(addShortcut);
  54. }
  55. }
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代码  

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.studio.android.ch10.ex1"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <application android:icon="@drawable/ji" android:label="@string/app_name">
  7. <activity android:name=".UrgentCall"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. <intent-filter>
  14. <action android:name="android.intent.action.CREATE_SHORTCUT" />
  15. </intent-filter>
  16. </activity>
  17. <activity android:name=".FireShortcut">
  18. <intent-filter>
  19. <action android:name="android.intent.action.CREATE_SHORTCUT" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. <uses-sdk android:minSdkVersion="3" />
  24. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
  25. </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> 

Java代码  

  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.os.Bundle;
  5. import android.os.Parcelable;
  6. public class FireShortcut extends Activity {
  7. @Override
  8. public void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. Intent addShortcut;
  11. //若是“添加快捷方式”的Action就初始化快捷方式的Intent
  12. if (getIntent().getAction()
  13. .equals(Intent.ACTION_CREATE_SHORTCUT)) {
  14. /*初始化添加快捷图标的Intent*/
  15. addShortcut = new Intent();
  16. addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
  17. "119");
  18. Parcelable icon = Intent.ShortcutIconResource.fromContext(
  19. this,R.drawable.huo);
  20. addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
  21. icon);
  22. Intent callFirePolice =
  23. new Intent(Intent.ACTION_CALL,Uri.parse("tel://119"));
  24. addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
  25. callFirePolice);
  26. /*设置Result*/
  27. //因为Action是由Launcher通过startActivityForResult这个方法发出的。
  28. setResult(RESULT_OK,addShortcut);
  29. } else {
  30. setResult(RESULT_CANCELED);
  31. }
  32. finish();
  33. }
  34. }
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”的快捷方式了。

时间: 2024-08-25 03:33:52

android 添加桌面快捷方式的相关文章

Ubuntu下安装ideaIU14并添加桌面快捷方式

1 安装jdk 这里以安装官方jdk1.7.79为例,jdk7网页:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html 1.1 下载jdk1.7.79 $ wget http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz 1.2 解压 $ tar -zxvf jdk-7u79-li

Android -- 创建桌面快捷方式

/** * * 返回添加到桌面快捷方式的Intent: * * 1.给Intent指定action="com.android.launcher.INSTALL_SHORTCUT" * * 2.给定义为Intent.EXTRA_SHORTCUT_INENT的Intent设置与安装时一致的action(必须要有) * * 3.添加权限:com.android.launcher.permission.INSTALL_SHORTCUT */ public Intent getShortcutT

Android判断桌面快捷方式是否存在

前两天做了个应用,需要实时获取桌面快捷方式是否存在,在某些第三方ROM下无法获取. 网上大量的例子都是谷歌原生系统或者小米.三星这类系统起作用,但是对于第三方ROM无法获取如:HTC.华为.一加.联想. 为什么不可以,试过的同学应该都知道,我这里就不一一解答,直接贴正确代码: import java.util.List; import android.content.ContentResolver; import android.content.Context; import android.c

Android中为你的应用程序添加桌面快捷方式

public void ShortCut(View view){ createDeskShortCut(this,getString(R.string.short_cut),R.drawable.ups_icon,"http://www.baidu.com"); createDeskShortCut(this,getString(R.string.short_cut2),R.drawable.ups_icont,"http://image.baidu.com/");

App添加桌面快捷方式

注释非常详细大家看吧,就是添加一个intent意图,然后发送广播就ok了 /** * 创建快捷方式 */ private void createShortCut() { // 创建快捷方式的Intent Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); // 不允许重复创建 shortcutIntent.putExtra("duplicate", f

CentOS-7.2添加桌面快捷方式

一,在桌面新建一个文件 文件名随意,但必须带有.desktop的后缀名. 1 gedit /home/zgw/Desktop/zgw.desktop 二,在文件中写入如下内容 1 [Desktop Entry] 2 Encoding=UTF-8 #编码格式 3 Exec=/home/zgw/squirrel-sql-3.7.1/squirrel-sql.sh #要执行程序的命令路径 4 Icon=/home/zgw/squirrel-sql-3.7.1/icons/acorn.ico #应用程序

Linux(Ubuntu)使用日记------为程序添加桌面快捷方式

 我们Ubuntu中的所以的程序的快捷方式都放在了/usr/share/applications文件夹下,都是以.desktop结尾的文件.我们可以在这个文件夹下创建我们的快捷方式,然后复制到桌面即可.我们以创建eclipse快捷方式为例. cd /usr/share/applications sudo vim eclipse.desktop eclipse.desktop文件内容如下,注意:”Exec”为执行文件位置,”Icon”为图标位置.一般只需修改这两个信息即可(当然如果是其他程序的快捷

android创建桌面快捷方式(启动目标非项目的启动页)

1.布局文件中,目标Activity加入以下filter <intent-filter>                  <action android:name="android.intent.action.CREATE_SHORTCUT" />                  <category android:name="android.intent.category.DEFAULT" />              &

Linux下添加桌面快捷方式

这里用Ubuntu下BurpSuite举例 sudo vim /home/user/Desktop/burpsuite.desktop //burpsuite随意起名,系统会系动创建文件 文件写入 #!/usr/bin [Desktop Entry] Name=Burpsuite Comment=Burpsuite Encoding=UTF-8 Exec=java -jar /burp.jar //这里直接写启动软件的方式,比如./msfconsole Terminal=false Type=A