2013-03-31 13 views
7

Mam trochę kodu, aby programowo usunąć wiadomości SMS z Androidem, ale kiedy spróbuję usunąć to w onReceive, nie zostanie usunięty żaden SMS.Usuń sms android po otrzymaniu bez opóźnienia

Przykładowy kod, aby usunąć sms

try { 
    // mLogger.logInfo("Deleting SMS from inbox"); 
    Uri uriSms = Uri.parse("content://sms/inbox"); 
    Cursor c = context.getContentResolver().query(
     uriSms, new String[] { "_id", "thread_id", "address", "person", 
     "date", "body" }, null, null, null); 

    if (c != null && c.moveToFirst()) { 
     do { 
      long id = c.getLong(0); 
      long threadId = c.getLong(1); 
      String address = c.getString(2); 
      String body = c.getString(5); 

      if (message.equals(body) && address.equals(number)) { 
       // mLogger.logInfo("Deleting SMS with id: " + threadId); 
       context.getContentResolver().delete(Uri.parse("content://sms/" + id), null, null); 
      } 
     } while (c.moveToNext()); 
    } 
} catch (Exception e) { 
    // mLogger.logError("Could not delete SMS from inbox: " + 
    // e.getMessage()); 
} 

Kiedy wklej to w onReceived to nowy SMS nie zostanie usunięty.

+0

Czy istnieje jakieś rozwiązanie? –

+0

@ user2229896 zaakceptuj odpowiedź, aby usunąć ją z listy bez odpowiedzi na stackoverflow. – Ayush

Odpowiedz

5

Trzeba dodać uprawnienia do pliku manifestu i zwiększyć priorytet swojego SMS Receiver klasy

<receiver android:name=".SMSReceiver" > 
    <intent-filter android:priority="1000"> 
     <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter> 
</receiver> 

To dlatego, że będzie to wywołać SMSReceiver przed wszelkimi operacjami poziomie systemu operacyjnego, takich jak (oszczędność SMS, powiadomienie, dźwięk SMS itd.).

Następnie w SMSReceiver onReceive() trzeba dodać abortBroadcast() aby przerwać wszelkie dalsze transmisje

public void onReceive(Context context, Intent intent) { 
    abortBroadcast(); 
} 

to wszystko

Cheers

Ayush Shah

0

Musisz usunąć sms za pomocą Smsid

getContentResolver().delete(Uri.parse("content://sms/" + smsid), null, null); 
Powiązane problemy