I'm trying to write an appication used to block sms/call from: sender in contact, filtering by number, content. I'm using receiver to handle it.
1. About block call function: What i did below:
@Override
public void onReceive(Context context, Intent intent) {
// get incomingNumber, ...etc
...
// check if need to block this call, if yes: drop call then clear log
if(isNeedToBlock(incomingNumber)) {
abortBroadcast();
telephonyService.endCall();
Utils.deleteLogFromNumber(incomingNumber);
}
}
public static void deleteLogFromNumber(String number) {
String queryString="NUMBER="+number;
context.getContentResolver().delete(CallLog.Calls.CONTENT_URI,queryString,null);
}
By this way, i can block a call now, but there are two issues:
- i can't delete the call logs. -> what im i doing wrong please?
- Some time, when receving a call (that should be blocked) screen will be turned on, even display the call for a short moment then drop. -> do we have any solution/suggestion?
2. About block sms function: As the code below:
public void onReceive(Context context, Intent intent) { // in SMS Receiver
this.context = context;
final Bundle extras = intent.getExtras();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
long _id = 0;
String address ="";
String messages = "";
// Get received SMS array
Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME);
for (int i = 0; i < smsExtra.length; ++i) {
SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
_id = sms.getIndexOnIcc(); // can't get sms id
address = sms.getOriginatingAddress();
messages += sms.getMessageBody().toString();
}
if (isNeedToBlock(address)) { // need to block
abortBroadcast();
deleteSms(address);
//markAsRead(address);
}
}
}, 2000); // delay 2s to make sure that sms was saved
}
I can delete this sms, but:
- Still an pop-up display this sms on screen (the popup from default sms composer on device)
- Can't get sms _id for deleting, i have to delete by "address" + some other condition to make sure that my app will not delete some old sms from the "address". (I have to delete the current one only)
- One more, i intend to create a option that make all blocked sms as read (don't delete), but i don't know where to start. => please give some hint on that.
To be more clearly, the manifest below:
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_LOGS"/>
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<receiver android:name=".CallReceiver" >
<intent-filter android:priority="2147483647" >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<receiver android:name=".SmsReceiver" android:enabled="true">
<intent-filter android:priority="2147483647">
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Please help me to give some hint, idea, solution, suggestion... for all of them. Many thanks!