添加桌面快捷方式创建功能
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Sun Jun 08 21:21:11 HKT 2025
|
#Thu Jul 24 00:58:40 GMT 2025
|
||||||
stageCount=1
|
stageCount=1
|
||||||
libraryProject=
|
libraryProject=
|
||||||
baseVersion=15.1
|
baseVersion=15.1
|
||||||
publishVersion=15.1.0
|
publishVersion=15.1.0
|
||||||
buildCount=0
|
buildCount=8
|
||||||
baseBetaVersion=15.1.1
|
baseBetaVersion=15.1.1
|
||||||
|
|||||||
@@ -3,6 +3,11 @@
|
|||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="cc.winboll.studio.numtable">
|
package="cc.winboll.studio.numtable">
|
||||||
|
|
||||||
|
<!-- Android 13及以下需要的权限 -->
|
||||||
|
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
|
||||||
|
<!-- 可选:查询快捷方式是否存在(部分机型需要) -->
|
||||||
|
<uses-permission android:name="com.android.launcher.permission.QUERY_ALL_PACKAGES" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:icon="@drawable/ic_launcher"
|
android:icon="@drawable/ic_launcher"
|
||||||
@@ -30,8 +35,6 @@
|
|||||||
android:name="android.max_aspect"
|
android:name="android.max_aspect"
|
||||||
android:value="4.0"/>
|
android:value="4.0"/>
|
||||||
|
|
||||||
<activity android:name=".GlobalApplication$CrashActivity"/>
|
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|||||||
@@ -1,9 +1,17 @@
|
|||||||
package cc.winboll.studio.numtable;
|
package cc.winboll.studio.numtable;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.pm.ShortcutInfo;
|
||||||
|
import android.content.pm.ShortcutManager;
|
||||||
|
import android.graphics.drawable.Icon;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.Button;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
import cc.winboll.studio.libappbase.LogView;
|
import cc.winboll.studio.libappbase.LogView;
|
||||||
|
import cc.winboll.studio.numtable.R;
|
||||||
import com.hjq.toast.ToastUtils;
|
import com.hjq.toast.ToastUtils;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
@@ -20,8 +28,64 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
mLogView = findViewById(R.id.logview);
|
mLogView = findViewById(R.id.logview);
|
||||||
|
|
||||||
ToastUtils.show("onCreate");
|
// 初始化创建快捷方式按钮
|
||||||
|
Button btnCreateShortcut = findViewById(R.id.btn_create_shortcut);
|
||||||
|
btnCreateShortcut.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
createDesktopShortcut();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建桌面快捷方式
|
||||||
|
*/
|
||||||
|
private void createDesktopShortcut() {
|
||||||
|
// 1. 创建启动目标Activity的Intent(通常是你的主Activity)
|
||||||
|
Intent targetIntent = new Intent(this, MainActivity.class); // 替换为你的主Activity
|
||||||
|
targetIntent.setAction(Intent.ACTION_MAIN);
|
||||||
|
targetIntent.addCategory(Intent.CATEGORY_LAUNCHER);
|
||||||
|
targetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
|
||||||
|
|
||||||
|
// 2. 创建快捷方式的Intent
|
||||||
|
Intent shortcutIntent = new Intent();
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
// Android 8.0及以上:使用ShortcutManager
|
||||||
|
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
|
||||||
|
if (shortcutManager.isRequestPinShortcutSupported()) {
|
||||||
|
// 创建快捷方式信息
|
||||||
|
ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(this, "unique_shortcut_id") // 唯一ID
|
||||||
|
.setShortLabel("应用快捷方式") // 短标签(显示在桌面)
|
||||||
|
.setLongLabel("我的应用快捷方式") // 长标签(长按显示)
|
||||||
|
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)) // 图标
|
||||||
|
.setIntent(targetIntent)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 发送创建请求
|
||||||
|
shortcutManager.requestPinShortcut(shortcutInfo, null);
|
||||||
|
ToastUtils.show("已请求创建快捷方式");
|
||||||
|
} else {
|
||||||
|
ToastUtils.show("当前设备不支持创建快捷方式");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Android 7.1及以下:使用旧版广播方式
|
||||||
|
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);
|
||||||
|
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "应用快捷方式"); // 快捷方式名称
|
||||||
|
// 设置图标(使用应用图标)
|
||||||
|
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
|
||||||
|
Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher));
|
||||||
|
// 标记为创建快捷方式
|
||||||
|
shortcutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
|
||||||
|
// 防止重复创建
|
||||||
|
shortcutIntent.putExtra("duplicate", false);
|
||||||
|
|
||||||
|
// 发送广播创建快捷方式
|
||||||
|
sendBroadcast(shortcutIntent);
|
||||||
|
ToastUtils.show("快捷方式已创建");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
|
|||||||
@@ -24,13 +24,22 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="0dp"
|
android:layout_height="0dp"
|
||||||
android:layout_weight="1.0"
|
android:layout_weight="1.0"
|
||||||
android:gravity="center_vertical|center_horizontal">
|
android:gravity="bottom">
|
||||||
|
|
||||||
<TextView
|
<LinearLayout
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:gravity="right">
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btn_create_shortcut"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="NumTable"
|
android:text="创建桌面快捷方式"
|
||||||
android:textAppearance="?android:attr/textAppearanceLarge"/>
|
android:layout_marginTop="16dp"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user