• 在桌面上创建快捷方式


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

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

  • 相关阅读:
    java数组转list
    【转载】tomcat端口被占用问题完美解决方案!
    基于apicloud的英语课堂app设计与实现
    springboot整合mybatis(SSM开发环境搭建)
    POI Excel读取图片对应位置和顺序生成图片方法
    E: Sub-process /usr/bin/dpkg returned an error code (1) 出错解决方案
    Ubuntu 虚拟机无法关机的解决方案
    Celery 提示[ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 61] Connection refused.
    Python @classmethod&@staticmethod 区别
    SyntaxError: non-default argument follows default argument
  • 原文地址:https://www.cnblogs.com/jiww/p/5647959.html
Copyright © 2020-2023  润新知