叶灼hua 2023-12-18 17:01 采纳率: 30.8%
浏览 8
已结题

AndroidStudio点击按钮,在手机桌面上添加一个(当前界面的)快捷方式

ManifestJava.xml添加了权限:

<uses-permission android:name="android.permission.UNINSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.INSTALL_SHORTCUT" />

我的MainActivity.java代码如下:


```java
package com.application.practicedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button=findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 在按钮点击事件中添加以下代码
                Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
                shortcutIntent.setAction(Intent.ACTION_MAIN);
                shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);

                Intent addIntent = new Intent();
                addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
                addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name");
                addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.map));
                addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
                addIntent.putExtra("duplicate", false);  // 不允许重复创建

                sendBroadcast(addIntent);
            }
        });
    }
}

我想实现的功能是:点击按钮,在手机桌面上添加一个(当前界面的)快捷方式,请问我该如何修改代码?

  • 写回答

1条回答 默认 最新

  • 小孟多 2023-12-18 17:14
    关注

    你的代码基本上是正确的,只需要在 shortcutIntent 中设置好对应的组件信息即可。修改一下点击按钮的代码如下:

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // 在按钮点击事件中添加以下代码
            Intent shortcutIntent = new Intent();
            shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), MainActivity.class));
            shortcutIntent.setAction(Intent.ACTION_MAIN);
            shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    
            Intent addIntent = new Intent();
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name");
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.map));
            addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
            addIntent.putExtra("duplicate", false);  // 不允许重复创建
    
            sendBroadcast(addIntent);
        }
    });
    
    
    

    其中,shortcutIntent 的设置改为:

    Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
    
    

    这里添加了两个标记:FLAG_ACTIVITY_NEW_TASK 表示启动一个新的任务栈,在桌面上打开快捷方式时能够正确地回到当前界面;FLAG_ACTIVITY_CLEAR_TOP 表示清空当前任务栈中其他的所有 Activity。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月18日
  • 创建了问题 12月18日