zoukankan      html  css  js  c++  java
  • 在桌面上创建快捷方式

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

    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"/>

  • 相关阅读:
    象限极角排序
    并查集与二部图
    POJ 1743 后缀数组不重叠最长重复子串
    POJ 2104 【主席树】【区间第K大】
    两串前缀最长后缀
    Codeforces 450D Jzzhu and Cities [heap优化dij]
    Codeforces 119C DP
    HDU 3068 [最长回文子串]
    Codeforces 132E Bits of merry old England 【最小费用最大流】
    ZOJ 3911Prime Query [素数处理 + 线段树]
  • 原文地址:https://www.cnblogs.com/jiww/p/5647959.html
Copyright © 2011-2022 走看看