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

วิธีลบข้อความ SMS จากกล่องขาเข้าโดยโปรแกรมใน Android

การลบข้อความ SMS จาก Inbox โดยโปรแกรมใน Android

ตัวอย่างนี้สาธิตวิธีใช้ ContentResolver ลบข้อความ SMS ทั้งหมดจากกล่องขาเข้า ต้องใช้ Permission READ_SMS และ WRITE_SMS

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

เปิด Android Studio เลือก File → New Project และกรอกข้อมูลที่จำเป็น

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/bt_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ลบ SMS"
        android:layout_centerInParent="true" />
</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ดหลัก (src/MainActivity.java)

import android.content.Context;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Context mContext;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mContext = this;
        Button btn = findViewById(R.id.bt_delete);
        
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (deleteSMS()) {
                    Toast.makeText(mContext, "ลบข้อความสำเร็จ", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(mContext, "ไม่สามารถลบข้อความได้", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    
    private boolean deleteSMS() {
        boolean isDeleted = false;
        try {
            // ลบ SMS ทั้งหมดจาก content://sms/
            mContext.getContentResolver().delete(Uri.parse("content://sms/"), null, null);
            isDeleted = true;
        } catch (Exception ex) {
            isDeleted = false;
            ex.printStackTrace();
        }
        return isDeleted;
    }
}

ขั้นตอนที่ 4: กำหนด Permission ใน AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

การทดสอบ

รันแอปพลิเคชัน ให้สิทธิ์ READ_SMS และ WRITE_SMS กดปุ่ม "ลบ SMS" จะลบข้อความทั้งหมด

วิธีลบข้อความ SMS จากกล่องขาเข้าโดยโปรแกรมใน Android

วิธีลบข้อความ SMS จากกล่องขาเข้าโดยโปรแกรมใน Android

⚠️ ข้อควรระวังสำคัญ

  • Android 4.4+ (KitKat): เฉพาะแอป SMS เริ่มต้น (Default SMS App) เท่านั้นที่มีสิทธิ์ WRITE_SMS
  • Android 10+ (API 29): การเข้าถึง SMS ถูกจำกัดมากขึ้น ต้องเป็น Default SMS App หรือใช้ Role Manager
  • Google Play Policy: แอปที่ขอสิทธิ์ SMS/Call Log ต้องผ่านการตรวจสอบเข้มงวด และต้องเป็นฟังก์ชันหลักของแอป
  • รหัสตัวอย่างนี้ลบ SMS ทั้งหมด ควรเพิ่มเงื่อนไข where clause เพื่อลบเฉพาะข้อความที่ต้องการ

ตัวอย่างลบเฉพาะข้อความบางรายการ

// ลบเฉพาะข้อความจากหมายเลขโทรศัพท์ tertentu
String where = "address = ?";
String[] whereArgs = new String[] {"+66812345678"};
getContentResolver().delete(Uri.parse("content://sms/"), where, whereArgs);