Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

Android สร้าง Shortcut บนหน้าจอ Home

ตัวอย่างนี้สาธิตวิธี สร้าง Shortcut บนหน้าจอ Home ใน Android (หมายเหตุ: หัวข้อเดิมระบุ Uninstall APKs แต่เนื้อหาเป็นการสร้าง Shortcut)

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่ใน Android Studio

ไปที่ File → New Project และกรอกรายละเอียดที่จำเป็นทั้งหมด

ขั้นตอนที่ 2: เพิ่มโค้ดใน res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:gravity="center"
        android:textSize="30sp"
        android:layout_height="wrap_content"
        android:text="Click here" />
</LinearLayout>

ใช้ TextView สำหรับทริกเกอร์การสร้าง Shortcut

ขั้นตอนที่ 3: เพิ่มโค้ดใน src/MainActivity.java

package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.pm.ShortcutInfoCompat;
import android.support.v4.content.pm.ShortcutManagerCompat;
import android.support.v4.graphics.drawable.IconCompat;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static final String PREF_KEY_SHORTCUT_ADDED = "Sairam";
    TextView text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = findViewById(R.id.text);
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ShortcutManagerCompat.isRequestPinShortcutSupported(getApplicationContext())) {
                    ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(getApplicationContext(), "#1")
                            .setIntent(new Intent(getApplicationContext(), MainActivity.class).setAction(Intent.ACTION_MAIN))
                            .setShortLabel("Test")
                            .setIcon(IconCompat.createWithResource(getApplicationContext(), R.drawable.logo))
                            .build();
                    ShortcutManagerCompat.requestPinShortcut(getApplicationContext(), shortcutInfo, null);
                } else {
                    Toast.makeText(MainActivity.this, "Launcher does not support shortcut icon", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

ขั้นตอนที่ 4: เพิ่มโค้ดใน AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.myapplication">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

การทดสอบแอปพลิเคชัน

รันแอปพลิเคชันบนอุปกรณ์ Android จริง ใน Android Studio คลิกไอคอน Run เลือกอุปกรณ์ แล้วตรวจสอบหน้าจอเริ่มต้น

Android สร้าง Shortcut บนหน้าจอ Home

คลิกที่ TextView จะขอสร้าง Shortcut บนหน้าจอ Home (ขึ้นอยู่กับ Launcher รองรับ)

คำอธิบายสำคัญ

  • ShortcutManagerCompat - รองรับหลาย API Level (Backward Compatibility)
  • isRequestPinShortcutSupported() - ตรวจสอบว่า Launcher รองรับ Pin Shortcut หรือไม่
  • ShortcutInfoCompat.Builder - สร้างข้อมูล Shortcut (ID, Intent, Label, Icon)
  • Intent.ACTION_MAIN - จำเป็นต้องตั้งค่า Action ให้ชัดเจน (โดยเฉพาะ Android 8.0+)
  • Permission INSTALL_SHORTCUT - จำเป็นสำหรับ Launcher เก่า (Legacy)

ประเภท Shortcut ใน Android

ประเภท คำอธิบาย API Level
Static Shortcuts กำหนดใน XML (res/xml/shortcuts.xml) 25+
Dynamic Shortcuts สร้าง/อัปเดต/ลบ ใน Runtime 25+
Pinned Shortcuts ผู้ใช้ปักหมุดไว้บน Home Screen 26+ (Compat ทำได้ก่อนหน้า)

หมายเหตุ

ไม่ทุก Launcher รองรับ Pinned Shortcut (เช่น Nova Launcher, Pixel Launcher รองรับ แต่บาง Custom ROM อาจไม่รองรับ) ควรมี Fallback UI หากไม่รองรับ