1 import android.Manifest;
2 android.content.Intent;
3 android.content.pm.PackageManager;
4 android.os.Parcelable;
5 android.support.v4.app.ActivityCompat;
6 android.support.v7.app.AppCompatActivity;
7 android.os.Bundle;
8 android.util.Log;
9 android.view.View;
10 android.widget.Button;
11
12 public class MainActivity extends AppCompatActivity {
13
14 private Button btn;
15 @Override
16 protected void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.activity_main);
19
20 btn = findViewById(R.id.btn);
21 btn.setOnClickListener(new View.OnClickListener() {
22 @Override
23 onClick(View view) {
24 //创建快捷方式
25 Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
26 action指定后,就决定了那个Receiver来执行这个消息
27 intent.putExtra("duplicate",true28 可以存在多个快捷方式
29 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"myapp"30 指定应用的名称
31 Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.mipmap.ic_launcher);
32 查找图标存到Parcelable类型
33 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
34 指定快捷方式的图标
35 Parcelable actionIntent = new Intent("com.xqz.shortcut"36 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,actionIntent);
37 sendBroadcast(intent);
38 Log.i("Tag","onClick: ----create"39 }
40 });
41 }
42 }