I would like to delete sms received from a phone number so quickly that it is not shown to the user (in order to "block" this phone number that spam the user). I also want my android app to show a layout but it seems to be impossible to do both at the same time!
This is my method to delete sms:
public void deleteSMS(Context context, String number) {
try {
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);
String address = c.getString(2);
if (address.equals(number)) {
context.getContentResolver().delete(
Uri.parse("content://sms/" + id), null, null);
Toast.makeText(this, "SMS deleted", Toast.LENGTH_SHORT).show();
}
} while (c.moveToNext());
}
} catch (Exception e) {
}
Then, to block the phone number "123456", I do :
setContentView(R.layout.activity_main);
int r = 0;
while (r<1){
deleteSMS(this, "123456");}
The app deletes sms perfectly, whenever I send one, but it doesn't show any layout or toasts. Also when I just do this:
setContentView(R.layout.activity_main);
deleteSMS(this, "123456");
It shows all the layout, toasts, and delete the sms. Do you have an idea to fix it ?
Just to give you a start since (after a number of comments) it seems you are intent on doing it where the app is always running)
public class MainActivity extends Activity{
private final static int SERVICE_ID = 1234;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent notificationIntent = new Intent(this, DeleteSmsService.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent. FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notice = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText("Deleting SMS Messages")
.setContentIntent(pendingIntent).build();
notice.flags |= Notification.FLAG_NO_CLEAR;
startForeground(SERVICE_ID, notice);
}
}
Then create a Service
public DeleteSmsService extends Service{
private ContentResolver contentResolver;
@Override
public void onCreate(){
super.onCreate();
contentResolver = getContext().getContentResolver();
new Thread(){
@Override
public void run(){
while(true){
deleteSms(contentResolver, "123456");
}
}
}.start();
}
public void deleteSMS(ContentResolver cr, String number) {
try {
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = cr.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);
String address = c.getString(2);
if (address.equals(number)) {
cr.delete(Uri.parse("content://sms/" + id), null, null);
Toast.makeText(this, "SMS deleted", Toast.LENGTH_SHORT).show();
}
} while (c.moveToNext());
}
} catch (Exception e) {
}
}
I haven't tested any of this, I have no clue if there are errors in it or if formatting may be off as I just typed it up really quick. This is to get you started doing what you want to do the best way I can think of doing it without bogging down the UI thread and maintaining normal conventions. You may need to utilize a thread handler, not sure (again haven't tested it).
Again, I would not do it this way, but this is just to help get you started with what you are trying to do since this is what you seem intent on doing.