简体   繁体   中英

How to “properly” send sms when call is received?

I am trying to have an Android Service listen for an incoming phone call and when one does occur, grab the incoming number and text it a message.

In my service I have made a PhoneStateListener:

TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener listener = new PhoneStateListener()
{
    @Override
    public void onCallStateChanged(int state, String incomingNumber)
    {
        switch(state)
        {
            case TelephonyManager.CALL_STATE_IDLE:
                Log.d(TAG, "Phone: Idle");
            break;
            case TelephonyManager.CALL_STATE_RINGING:
                Log.d(TAG, "Phone: Ringing.");
                Log.i(TAG, "Incoming call from: " + incomingNumber);
                sendSms();
            break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.d(TAG, "Phone: Off Hook");
            break;
        }
    }
};
tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

My sendSms() function is as follows:

private void sendSms()
{
    SmsManager manager = SmsManager.getDefault();

    wakeLock.acquire();
    Log.d(TAG, "Wake Lock Acquired!");

    if (getMessageContent(getInformStatus()).length() > 160)
    {
        ArrayList<String> messagelist = manager.divideMessage(getMessageContent(getInformStatus()));

        manager.sendMultipartTextMessage(getReturnAddress(), null, messagelist, null, null);
        Log.i(TAG, "Multipart Text Message Sent!");
    }
    else
    {
        manager.sendTextMessage(getReturnAddress(), null, getMessageContent(getInformStatus()), sentPI, null);
        Log.i(TAG, "Text Message Sent!");
    }

    wakeLock.release();
    Log.d(TAG, "Wake Lock Released!");
}

I even have an SMS_SENT Broadcast Receiver to check if the text message sent out correctly that will recall the sendSms() function if it did not sent for whatever reason:

resend = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        switch (getResultCode())
        {
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Log.e(TAG, "Text did NOT send (GENERIC_FAILURE)");
                Log.i(TAG, "Attempting to resend");
                sendSms();
            break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Log.e(TAG, "Text did NOT send (NO_SERVICE)");
                Log.i(TAG, "Attempting to resend");
                sendSms();
            break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Log.e(TAG, "Text did NOT send (RADIO_OFF)");
                Log.i(TAG, "Attempting to resend");
                sendSms();
            break;
        }
    }
};
registerReceiver(resend, new IntentFilter("android.provider.Telephony.SMS_SENT"));

This works on my phone fine, however on other people's phones it likes to sometimes toggle back and forth between RINGING and IDLE in the PhoneStateListener a few times before "sending".. The phone actually says that the application is trying to send multiple text messages and asks the user if that is okay. When they click yes, it just asks again.

To make it better, it never actually sends the text message either..

On another phone, it doesn't do this at all.. The other phone will go through the motions and the logcat is identical to mine.. It actually says it sends, but the recipient never gets the SMS..

It is suppose to be an auto-response service of sorts and I wanted to try and make it work on at least Froyo, Gingerbread, and Ice Cream Sandwich..

By the way, it works 100% on my:
Galaxy Nexus (toro) (Android 4.0.3)
Droid 1 (sholes) (Android 2.3.7)
HTC Thunderbolt (mecha) (Android 2.3.7)

However, if it's my friend's:
Nexus S 4G (crespo4g) (Android 4.0.3)
or my other friend's
Galaxy Nexus (toro) (Android 4.0.3)

It does not work..

What I really don't understand is that it doesn't work with my friend with the exact same phone as me..

I feel like this code implementation is a hit or miss and was wondering if anyone had some insight to help out.

Appreciate it!

I think I figured it out. What I ended up doing is putting a Handler that called a Runnable 30 seconds after the phone call was initially received.. This gave the phone time to finish the phone call (assuming it was in your pocket) and then cleanly send the text message..

I hope this is the only issues I was having, but it seems to work on my friends Galaxy Nexus (toro) now..

Hope this helps anyone else wondering about this..

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM