当前位置:操作系统 > 安卓/Android >>

Android桌面快捷方式

Android中的桌面快捷方式和PC机上的快捷方式一样,用于启动某一应用程序。要在桌面添加一个快捷方式非常简单,只需长按桌面或者点击"Menu"按钮,然后在弹出的选项中选择shortcut,然后选择要添加的快捷方式即可。
下面主要介绍如何通过代码将一个应用程序添加到桌面快捷方式。
首先在描述文件AndroidManifest.xml中注册一个action为:<action android:name="android.intent.action.CREATE_SHORTCUT"/>
如下所示:
[java]
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.test.shortcut" 
      android:versionCode="1" 
      android:versionName="1.0"> 
 
 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
        <activity android:name=".MainActivity" 
                  android:label="@string/app_name"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" /> 
                <action android:name="android.intent.action.CREATE_SHORTCUT"/> 
            </intent-filter> 
        </activity> 
    </application> 
</manifest> 

接下来是MainActivity:
[java]
public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        Intent intent; 
        //判断是否要添加快捷方式 
        if (this.getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)){ 
            intent = new Intent(); 
            //设置快捷方式名称 
            intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "拨打电话"); 
             
            //设置快捷方式图标 
            Parcelable icon = Intent.ShortcutIconResource.fromContext(this, android.R.drawable.stat_sys_phone_call); 
            intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); 
             
            //设置快捷方式执行的intent 
            Uri uri = Uri.parse("tel:055555");   
            Intent it = new Intent(Intent.ACTION_DIAL, uri);   
            intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, it); 
             
            setResult(RESULT_OK,intent); 
        }else { 
            //取消 
            setResult(RESULT_CANCELED);; 
        } 
        this.finish(); 
    } 



 代码非常简单,运行程序,长按桌面,选择shortcut后如图所示:

选择将ShortCut添加到桌面,效果如图:

单击“拨打电话”图标,出现如图所示结果:

 



摘自 fwwdn的专栏
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,