Get SMS Delivery Report with in service

▼魔方 西西 提交于 2021-01-29 08:17:10

问题


I try to send sms when my mobile get rebooted.My code is working in activity,I can get the delivery report in activity.But I put the same code in Broadcastreceiver, It is not working.

                   try 
                     {

                        String SENT      = "SMS_SENT";


                    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);

                    registerReceiver(new BroadcastReceiver() 
                    {
                        @Override
                        public void onReceive(Context arg0, Intent arg1) 
                        {
                            int resultCode = getResultCode();
                            switch (resultCode) 
                            {


                            case Activity.RESULT_OK:                 
                                 Toast.makeText(getBaseContext(), "SMS sent",Toast.LENGTH_LONG).show();
                                                                           break;
                            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:  Toast.makeText(getBaseContext(), "Generic failure",Toast.LENGTH_LONG).show();
                                                                           break;
                            case SmsManager.RESULT_ERROR_NO_SERVICE:       Toast.makeText(getBaseContext(), "No service",Toast.LENGTH_LONG).show();
                                                                           break;
                            case SmsManager.RESULT_ERROR_NULL_PDU:         Toast.makeText(getBaseContext(), "Null PDU",Toast.LENGTH_LONG).show();
                                                                           break;
                            case SmsManager.RESULT_ERROR_RADIO_OFF:        Toast.makeText(getBaseContext(), "Radio off",Toast.LENGTH_LONG).show();
                                                                           break;
                            }
                        }
                    }, new IntentFilter(SENT));

                               SmsManager smsMgr = SmsManager.getDefault();
                               smsMgr.sendTextMessage(address, null,"Send sms", sentPI, null);

                                }
                     catch (Exception e) 
                     {
                        Toast.makeText(this, e.getMessage()+"!\n"+"Failed to send SMS", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                     }

How to use this code with in broadcastreceiver or in service?.


回答1:


You cannot register a BroadcastReceiver in a BroadcastReceiver because the BroadcastReceiver does not live very long (only for the duration of the call to onReceive(). So what you need to do is to start a Service from your BroadcastReceiver and have it send the SMS. The code you have should work from a Service.




回答2:


Dont use SmsManager.RESULT_ERROR_GENERIC_FAILURE: since its deprecated instead use android.telephony.SmsManager Docs

Once reboot call the Broadcast Reciever:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    Intent service = new Intent(context, YourService.class);
    context.startService(service);
  }
} 

Your Service:

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.SmsManager;
import android.widget.Toast;

public class YourService extends Service {


 @Override
 public void onCreate() {

 }

 @Override
 public IBinder onBind(Intent arg0) {
  return null;
 }

 @Override
 public void onDestroy() {
   super.onDestroy();
 }

 @Override
 public void onStart(Intent intent, int startId) {
  super.onStart(intent, startId);

  //
  SmsManager smsManager = SmsManager.getDefault();
  smsManager.sendTextMessage(number to send , null, text, null, null);
 }

 @Override
 public boolean onUnbind(Intent intent) {

 }

}

Make Sure u add the required permission,receiver and service in Manifest File



来源:https://stackoverflow.com/questions/18308062/get-sms-delivery-report-with-in-service

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