Hi, guys,
I made a app to block some junk messages. That app would delete the messages from the senders specified in the ban list. That app works in a way, but has some defects, as follows,
1. Sometime it just doesn't work, missing the new message to delete.
2. Even if it works deleting the message, a notification sound and message still occur.
Therefore, could any improvement be made to 100% block messages without any notifications?
Please check the codes below, and see what I can do.
Thank you.
========================================================================================
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class MySMSReceiver extends BroadcastReceiver
{
private static final String mACTION = "android.provider.Telephony.SMS_RECEIVED";
private String str_numbers, current_number;
private int killed;
public static final String PREFS_NAME = "LEONARDO_JUNKILLER_PREFS";
private String[] numbers;
Cursor cursor;
@Override
public void onReceive(Context context, Intent intent)
{
//get the banned numbers list from application's preference
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
str_numbers = settings.getString("NUMBERS", null);
killed = settings.getInt("KILLED", 0);
if (str_numbers == null)
{
return;
} else
{
numbers = str_numbers.split("\\s+");
for(String number:numbers)
{
number.trim();
}
}
//check if it is a message
if (intent.getAction().equals(mACTION))
{
// get the content from the intent
Bundle bundle = intent.getExtras();
if (bundle != null)
{
Object[] myOBJpdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[myOBJpdus.length];
for (int i = 0; i < myOBJpdus.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) myOBJpdus[i]);
}
//for each message in the intent, check if it is a junk message and delete it if so.
for (SmsMessage currentMessage : messages)
{
// get the sender's number for that message
current_number = currentMessage.getDisplayOriginatingAddress();
// if the number is in the ban list, then find and delete all of the received messages belonging to that number, including the new one and existing ones.
if (toKill(current_number))
{
cursor = context.getContentResolver().query(
Uri.parse("content://sms/"),
new String[]{"_id", "thread_id", "address"},
"address=\"" + current_number + "\"",
//"address=" + current_number,
null,
null);
if (cursor.moveToFirst())
{
do
{
Log.i("JUNKILLER", "The message id:" + cursor.getString(0) + ";address:" + cursor.getString(2) + " is deleted");
Uri mUri = Uri.parse("content://sms/" + cursor.getString(0));
context.getContentResolver().delete(mUri, null, null);
killed++;//count the deleted messages for a banned number.
} while (cursor.moveToNext());
}
cursor.close();
}
}
}
}
SharedPreferences.Editor editor = settings.edit();
editor.putInt("KILLED", killed);
editor.commit();
}
private boolean toKill(String number_check)
{
for (String number : numbers)
{
if (number_check.indexOf(number) >= 0)
{
return true;
}
}
return false;
}
}
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
No comments:
Post a Comment