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

วิธีถอนการติดตั้ง APK โดยโปรแกรม (Uninstall APK Programmatically)

บทความนี้สาธิตวิธีการเรียกใช้ Intent มาตรฐานของ Android เพื่อถอนการติดตั้งแอปพลิเคชัน (Uninstall) โดยโปรแกรม ผู้ใช้จะเห็น Dialog ยืนยันก่อนถอนติดตั้ง

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

Android Studio → File → New Project → Empty Activity

ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="กดที่นี่เพื่อถอนติดตั้งแอปนี้"
        android:textSize="24sp"
        android:padding="24dp"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"/>
</LinearLayout>

ขั้นตอนที่ 3: MainActivity.java

package com.example.uninstallapp;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private TextView textView;
    private static final String TARGET_PACKAGE = "com.example.uninstallapp"; // Package ของตัวเอง

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

        textView = findViewById(R.id.text);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uninstallApp();
            }
        });
    }

    private void uninstallApp() {
        // Intent สำหรับถอนการติดตั้ง
        Intent intent = new Intent(Intent.ACTION_DELETE);
        // ระบุ Package name ที่จะถอน (ใช้ package: scheme)
        intent.setData(Uri.parse("package:" + TARGET_PACKAGE));
        
        // ใส่ Flag ให้กลับมาที่แอปหลังถอนติดตั้ง (ถ้าไม่ถอนก็กลับมา)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        
        try {
            startActivity(intent);
        } catch (Exception e) {
            Toast.makeText(this, "ไม่สามารถเปิดหน้าถอนติดตั้งได้: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
}

ขั้นตอนที่ 4: AndroidManifest.xml

<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.uninstallapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="Uninstall Demo"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

🔑 จุดสำคัญ

  1. Intent.ACTION_DELETE - Action มาตรฐานสำหรับลบ/ถอนติดตั้ง
  2. Uri.parse("package:com.example.app") - ระบุ Package Name ที่จะถอน
  3. ผู้ใช้ต้องยืนยัน - ระบบจะแสดง Dialog ให้ผู้ใช้กด "ถอนติดตั้ง" หรือ "ยกเลิก" ไม่สามารถถอนได้โดยอัตโนมัติโดยไม่มี Root
  4. ไม่ต้องการ Permission เพิ่มเติม - ใช้ Intent มาตรฐานของระบบ

🎯 กรณีการใช้งาน (Use Cases)

  • ปุ่ม "ถอนติดตั้งแอปเก่า" ในแอปอัปเดตเวอร์ชันใหม่
  • แอปจัดการแอปพลิเคชัน (App Manager)
  • ฟีเจอร์ "Switch to Pro Version" - ถอน Free Version
  • ทดสอบการติดตั้ง/ถอนติดตั้งในระหว่าง Development

⚠️ ข้อจำกัด

  • ไม่สามารถถอนติดตั้งแอปอื่นได้โดยอัตโนมัติ - ต้องการการยืนยันจากผู้ใช้เสมอ
  • ไม่ทำงานกับ System Apps - ไม่สามารถถอนแอประบบได้
  • ต้องรู้ Package Name - ต้องระบุ Package Name ที่ถูกต้อง
  • Device Owner / Profile Owner: เฉพาะแอปที่เป็น Device Admin เท่านั้นที่ถอนติดตั้งได้โดยไม่ต้องการยืนยัน (ใช้ PackageManager.deletePackage())

🔧 การถอนติดตั้งแอปอื่น (Uninstall Other Apps)

หากต้องการถอนแอปอื่น ให้เปลี่ยน TARGET_PACKAGE เป็น Package Name ของแอปเป้าหมาย:

// ตัวอย่าง: ถอนติดตั้ง Facebook
String targetPackage = "com.facebook.katana";
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:" + targetPackage));
startActivity(intent);

📱 การทดสอบ

  1. Build และ Install แอปบนอุปกรณ์
  2. เปิดแอป กดข้อความ "กดที่นี่เพื่อถอนติดตั้งแอปนี้"
  3. ระบบจะแสดง Dialog "คุณต้องการถอนติดตั้งแอปพลิเคชันนี้หรือไม่?"
  4. กด "ถอนติดตั้ง" → แอปจะถูกลบออก
หมายเหตุ: หากทดสอบถอนติดตั้งตัวเอง แอปจะหายไปทันทีหลังยืนยัน ควร Backup โค้ดไว้ก่อนทดสอบ!