0
我已經寫了一個代碼來刪除短信從收件箱&塊短信從一個特定的號碼。但它並不完美。 這裏是清單文件,我設置優先2147483647(最大INT數)短信不會阻止或刪除從android
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dotnet.epomoload">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:enabled="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
<receiver android:name="com.example.dotnet.epomoload.BroadCastReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
下面是代碼,在這種情況下廣播功能無法正常工作。但是我得到的數據。 &我不明白爲什麼代碼無法正常工作。
@SuppressWarnings("deprecation")
public class BroadCastReceiver extends BroadcastReceiver
{
public String blockingNumber = "BulkSMS";
private static final String LOG_TAG = "SMSApp";
/* package */
static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
public void onReceive(Context context, Intent intent){
String strFrom = "";
String strMsg="";
if (intent.getAction().equals(ACTION)){
Bundle bundle = intent.getExtras();
if (bundle != null){
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++){
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
for (SmsMessage message : messages){
strFrom = message.getDisplayOriginatingAddress();
strMsg = message.getDisplayMessageBody();
}
if (strFrom.equals(blockingNumber)){
this.abortBroadcast();
Toast.makeText(context.getApplicationContext(),"Sms From Shohan"+strMsg,Toast.LENGTH_LONG).show();
//removeMessage(context,blockingNumber);
Uri uriSMS = Uri.parse("content://sms/inbox");
Cursor cursor = context.getContentResolver().query(uriSMS, null,
null, null, null);
cursor.moveToFirst();
if(cursor.getCount() > 0){
int ThreadId = cursor.getInt(1);
context.getContentResolver().delete(Uri.
parse("content://sms/conversations/"+ThreadId), "address=?",
new String[]{blockingNumber});
Log.d("Message Thread Deleted", blockingNumber);
}
cursor.close();
}
}
}
}
}
由於KitKat,除非將它選作用戶的默認消息應用程序,並且'abortBroadcast()'不再適用於'SMS_RECEIVED'廣播,否則您的應用程序無法刪除消息。 –