1

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!

Lạng Hoàng
  • 1,790
  • 3
  • 17
  • 32
  • You need to find a way to put your sms receiver to highest priority ,so any broadcast will be received by your receiver first than any other receiver – meShakti Oct 30 '14 at 11:21
  • @meShakti: yes, but i already do it by set the priority as the code above but it still happen. Are there any other way to do that? – Lạng Hoàng Oct 30 '14 at 11:30
  • i see what you did . refer http://stackoverflow.com/questions/16944942/not-get-sms-even-when-set-the-highest-priority-and-installed-first Although it won't solve the problem but it suggest priority can never be greater than 1000. – meShakti Oct 31 '14 at 05:05
  • I have further researched into the topic . after kitkat release, only the default sms application can recieve sms first and block broadcast . in other words if your app tries to block broadcast of default app it will not work . you have to make your application default sms application. – meShakti Oct 31 '14 at 05:19
  • here is some referrence that can help : 1.http://stackoverflow.com/questions/21720657/how-to-set-my-sms-app-default-in-android-kitkat 2.http://stackoverflow.com/questions/18940286/how-to-make-my-sms-app-is-highest-priority-to-receive-broadcast-receiver – meShakti Oct 31 '14 at 05:19
  • i'm testing on fw 4.0 not kitkat. I'm still in those topics given by you and still google for solution. Meanwhile, i have another issues in this link: http://stackoverflow.com/questions/26679881/how-to-handle-multiple-calls-at-time-in-android could you help to take a look, please? – Lạng Hoàng Oct 31 '14 at 17:03

0 Answers0