Android send sms pending Intent

自作多情 提交于 2020-01-06 19:24:47

问题


I'm using the following code to send SMS in my android app:

    PendingIntent pending = PendingIntent.getBroadcast(activity, 0, new Intent(PENDING), 0);
    activity.registerReceiver(new BroadcastReceiver() {
        public void onReceive(Context arg0, Intent intent) {
            switch (getResultCode()) {
                case Activity.RESULT_OK: {
                    sendJsonData(false);
                    break;
                }

                case SmsManager.RESULT_ERROR_GENERIC_FAILURE: {
                    sendJsonData(true);
                    break;
                }

                case SmsManager.RESULT_ERROR_NO_SERVICE: {
                    sendJsonData(true);
                    break;
                }

                case SmsManager.RESULT_ERROR_NULL_PDU: {
                    sendJsonData(true);
                    break;
                }

                case SmsManager.RESULT_ERROR_RADIO_OFF: {
                    sendJsonData(true);
                    break;
                }
            }
        }

        private void sendJsonData(boolean error) {
        }
     }, new IntentFilter(PENDING));

    PendingIntent deliveryIntent = PendingIntent.getBroadcast(activity, 0, new Intent(DELIVERED), 0);
    activity.registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Log.i(TAG, "got here");
                    break;
                case Activity.RESULT_CANCELED:
                    Log.i(TAG, "got here");
                    break;
            }
        }
    }, new IntentFilter(DELIVERED));

My question is when I get RESULT_OK, how can I know the _id column of the cursor adapter? I need this ID later. I can check for the _id later but is there a way to get it from the pendingIntent?

I'm not getting any callback on the deliveryIntent after delivery.


回答1:


My question is when I get RESULT_OK, how can I know the _id column of the cursor adapter?

You can put a ContentObserver on the SMS Provider in order to receive a callback when the message gets written, and then query the Provider to get the message ID. I've an example of how to do that in my answer here. You would just need to change "thread_id" to "_id".

I can check for the _id later but is there a way to get it from the pendingIntent?

Unfortunately, no. That Intent is not going to carry much information beyond the result code. It might have a few extras attached - e.g., the SIM slot number - but it won't have the ID assigned by the SMS ContentProvider.

I'm not getting any callback on the deliveryIntent after delivery.

That's not unusual. The delivery PendingIntent will only fire if confirmation is received from the service center, but not every wireless provider offers that functionality.



来源:https://stackoverflow.com/questions/36204990/android-send-sms-pending-intent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!