Giới thiệu
Hướng dẫn này trình bày cách xóa tin nhắn SMS khỏi hộp thư đến (inbox) theo chương trình trong Android sử dụng ContentResolver.
Các bước thực hiện
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.
Bước 2: Thiết kế giao diện (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="Xóa SMS"
android:layout_centerInParent="true" />
</RelativeLayout>
Bước 3: Viết logic xử lý (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(v -> {
if (deleteSMS()) {
Toast.makeText(mContext, "Tin nhắn đã được xóa.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(mContext, "Không thể xóa tin nhắn.", Toast.LENGTH_LONG).show();
}
});
}
private boolean deleteSMS() {
boolean isDeleted = false;
try {
// Xóa TẤT CẢ tin nhắn (cẩn thận!)
mContext.getContentResolver().delete(
Uri.parse("content://sms/"),
null,
null
);
isDeleted = true;
} catch (Exception ex) {
isDeleted = false;
ex.printStackTrace();
}
return isDeleted;
}
}
Bước 4: Cấu hình Manifest (androidManifest.xml) - Cần quyền
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<!-- Quyền bắt buộc -->
<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>
⚠️ CẢNH BÁO QUAN TRỌNG
- Code hiện tại xóa TẤT CẢ tin nhắn (whereClause = null, whereArgs = null)
- Từ Android 4.4 (KitKat), chỉ ứng dụng SMS mặc định mới có quyền WRITE_SMS
- Cần xin quyền runtime (runtime permission) cho READ_SMS và WRITE_SMS từ API 23+
- Google Play hạn chế بشدة ứng dụng yêu cầu quyền SMS/Call Log
Cách xóa an toàn hơn (có điều kiện)
// Chỉ xóa tin nhắn từ số cụ thể
String where = "address = ?";
String[] args = new String[]{"0909123456"};
getContentResolver().delete(Uri.parse("content://sms/"), where, args);
// Hoặc xóa tin nhắn cũ hơn 30 ngày
String where = "date < ?";
String[] args = new String[]{String.valueOf(System.currentTimeMillis() - 30L*24*60*60*1000)};
getContentResolver().delete(Uri.parse("content://sms/"), where, args);
Chạy ứng dụng
Kết nối thiết bị Android, cấp quyền SMS khi yêu cầu, nhấn Run trong Android Studio. Nhấn "Xóa SMS" - sẽ xóa toàn bộ tin nhắn!

