zoukankan      html  css  js  c++  java
  • 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.   
    10. public class UrgentCall extends Activity implements   
    11.     OnClickListener {  
    12.   
    13.     Button police;  
    14.     Button fire;  
    15.     Intent directCall;  
    16.     private final String ACTION_ADD_SHORTCUT =  
    17.         "com.android.launcher.action.INSTALL_SHORTCUT";  
    18.       
    19.     @Override  
    20.     public void onCreate(Bundle savedInstanceState) {  
    21.         super.onCreate(savedInstanceState);  
    22.           
    23.         setContentView(R.layout.main);  
    24.           
    25.         police = (Button)findViewById(R.id.police);  
    26.         fire = (Button)findViewById(R.id.firepolice);  
    27.           
    28.         police.setOnClickListener(this);  
    29.         fire.setOnClickListener(this);  
    30.           
    31.         directCall = new Intent(Intent.ACTION_CALL);  
    32.     }  
    33.   
    34.     @Override  
    35.     public void onClick(View v) {  
    36.         Intent addShortcut =   
    37.             new Intent(ACTION_ADD_SHORTCUT);  
    38.         String numToDial = null;  
    39.         Parcelable icon = null;  
    40.         switch (v.getId()) {  
    41.         case R.id.police:  
    42.             numToDial = "110";  
    43.             icon = Intent.ShortcutIconResource.fromContext(  
    44.                     this,R.drawable.jing);  
    45.             break;  
    46.         case R.id.firepolice:  
    47.             numToDial = "119";  
    48.             icon = Intent.ShortcutIconResource.fromContext(  
    49.                     this,R.drawable.huo);  
    50.             break;  
    51.         default:  
    52.             break;  
    53.         }  
    54.         addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,   
    55.             numToDial);  
    56.         directCall.setData(Uri.parse("tel://"+numToDial));  
    57.         addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,   
    58.                 directCall);  
    59.         addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,   
    60.                 icon);  
    61.         sendBroadcast(addShortcut);  
    62.     }  
    63. }  
    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.   
    7. public class FireShortcut extends Activity {  
    8.       
    9.     @Override  
    10.     public void onCreate(Bundle savedInstanceState) {  
    11.         super.onCreate(savedInstanceState);  
    12.         Intent addShortcut;  
    13.         //若是“添加快捷方式”的Action就初始化快捷方式的Intent  
    14.         if (getIntent().getAction()  
    15.                 .equals(Intent.ACTION_CREATE_SHORTCUT)) {  
    16.               
    17.             /*初始化添加快捷图标的Intent*/  
    18.             addShortcut = new Intent();  
    19.             addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,   
    20.                     "119");  
    21.               
    22.             Parcelable icon = Intent.ShortcutIconResource.fromContext(  
    23.                     this,R.drawable.huo);  
    24.             addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,   
    25.                     icon);  
    26.               
    27.             Intent callFirePolice =   
    28.                 new Intent(Intent.ACTION_CALL,Uri.parse("tel://119"));  
    29.             addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,  
    30.                     callFirePolice);  
    31.               
    32.             /*设置Result*/  
    33.             //因为Action是由Launcher通过startActivityForResult这个方法发出的。  
    34.             setResult(RESULT_OK,addShortcut);  
    35.         } else {  
    36.             setResult(RESULT_CANCELED);  
    37.         }  
    38.         finish();  
    39.     }  
    40. }  
    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”的快捷方式了。

  • 相关阅读:
    集合set
    字典
    元组
    列表
    for循环
    Windows调试2.异常产生详细流程
    双机环境搭建
    Windows调试1.WinDbg基本使用-异常基础知识
    PE基础7-HOOK练习
    PE基础6_远程线程注入-HOOK(消息-InLine-IAT)
  • 原文地址:https://www.cnblogs.com/xiaochao1234/p/4104497.html
Copyright © 2011-2022 走看看