我想通过点击特定列表视图来删除收件箱短信。我尝试了很多,但无法做到这一点。 Android版本是4.1.2。善意的帮助。我已通过此功能显示收件箱:
public void refreshInbox(){
ContentResolver contentResolver = getContentResolver(); // get content resolver to select database
Cursor inboxCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);
int indexAddress = inboxCursor.getColumnIndex("address"); //get index of address column
int indexBody = inboxCursor.getColumnIndex("body"); //get index of message body column
if(indexBody < 0 || !inboxCursor.moveToFirst()) {return;};
arrayAdapter.clear();
do {
String str = "Sms from: " +inboxCursor.getString(indexAddress) + "\n"
+ inboxCursor.getString(indexBody);
arrayAdapter.add(str);
} while (inboxCursor.moveToNext());
}
并尝试通过此功能单击列表视图来删除短信:
ContentResolver contentResolver = getContentResolver(); // get content resolver to select database
Cursor c = contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);
//int idd = c.getInt(0);
int thread_id = c.getInt(1); //get the thread_id
getContentResolver().delete(Uri.parse("content://sms/" + thread_id),null,null);
AndroidManifist:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zohaib.ultimatesmsblocker">
<!-- Give permission to read, receive, send sms -->
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".IncomingSms">
<intent-filter android:priority="10000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name=".conversation"
android:label="@string/title_activity_conversation"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Blocklist"
android:label="@string/title_activity_blocklist"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
</manifest>