码迷,mamicode.com
首页 > 其他好文 > 详细

在桌面上创建快捷方式

时间:2016-07-06 21:27:48      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

在程序中把一个软件的快捷方式添加到桌面上,只需要如下三步:

1、创建一个添加快捷方式的Intent,该Intent的Action属性值应该为com.android.launcher.action.INSTALL_SHORTCUT.

2、通过为该Intent添加Extra属性来设置快捷方式的标题、图标及快捷方式对应启动的程序。

3、调用sendBroadcast()方法发送广播即可添加快捷方式。

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class AddShortCut extends Activity {
  ImageView flower;
  //定义两份动画资源
  Animation anim;
  Animation reverse;
  final Handler handler = new Handler(){
    public void handleMessage(Message msg) {
      if(msg.what == 0x123){
        flower.startAnimation(reverse);
      }
    }
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_short_cut);
    flower = (ImageView) findViewById(R.id.flower);
    //加载第一份动画资源
    anim = AnimationUtils.loadAnimation(this,R.anim.anim);
    //设置动画结束后保留结束状态
    anim.setFillAfter(true);
    //加载第二份动画资源
    reverse = AnimationUtils.loadAnimation(this, R.anim.reverse);
    //设置动画结束后保留结束状态
    reverse.setFillAfter(true);
    Button start = (Button) findViewById(R.id.start);
    //为按钮的单击事件添加监听器
    start.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        //创建添加快捷方式的Intent
        Intent addIntent =
          new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        String title = getResources().getString(R.string.title);
        //加载快捷方式的图标
        Parcelable icon = Intent.ShortcutIconResource
              .fromContext(AddShortCut.this, R.drawable.icon);
        //创建点击快捷方式后操作Intent,该出当点击创建的快捷方式后,再次启动该程序
        Intent myIntent = new Intent(AddShortCut.this, AddShortCut.class);
        //设置快捷方式的标题
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
        //设置快捷方式的图标
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
        //设置快捷方式对应的Intent
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);
        //发送广播添加快捷方式
        sendBroadcast(addIntent);
      }
    });
  }
}

在程序中添加快捷方式需要相应的权限,在AndroidManifest.xml文件中添加如下配置片段:

<!-- 指定添加安装快捷方式的权限 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

在桌面上创建快捷方式

标签:

原文地址:http://www.cnblogs.com/jiww/p/5647959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!