Android桌面快捷方式

Android中的桌面快捷方式和PC机上的快捷方式一样,用于启动某一应用程序。要在桌面添加一个快捷方式非常简单,只需长按桌面或者点击"Menu"按钮,然后在弹出的选项中选择shortcut,然后选择要添加的快捷方式即可。
下面主要介绍如何通过代码将一个应用程序添加到桌面快捷方式。
首先在描述文件AndroidManifest.xml中注册一个action为:<action android:name="android.intent.action.CREATE_SHORTCUT"/>
如下所示:
<?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:
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添加到桌面,效果如图:


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



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值