2017-01-13 27 views
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(); 
      } 
     } 
    } 

} 


} 
+3

由于KitKat,除非将它选作用户的默认消息应用程序,并且'abortBroadcast()'不再适用于'SMS_RECEIVED'广播,否则您的应用程序无法删除消息。 –

回答

0

从Android 4.4开始,应用程序无法对短信进行写操作(标记为已读,删除)。要实现这种行为,您的应用程序必须是默认的短信消息应用程序。

但是,如果你的应用程序被设计成表现为默认的短信应用, 然后在您的应用程序没有被选择作为默认,这是您了解放置在您的应用程序的限制和禁用 功能一样重要 适当。虽然系统会将已发送的SMS消息 写入SMS提供程序,而您的应用程序不是默认的SMS应用程序,但它不会写入已发送的MMS消息,并且您的应用程序无法将其写入 SMS提供程序以进行其他操作,例如将邮件标记为草稿, 它们标记为已读,删除等 Reference

当您的应用程序设置为默认应用程序,您必须检查Telephony.Sms.getDefaultSmsPackage()