不会调用BroadcastReceiver的onReceive()内部的自定义方法

时间:2019-07-19 20:28:59

标签: android broadcastreceiver

我正在使用BroadcastReceiver侦听传入的消息。当有新消息到达时,我会收到Toast,但是除toast和从BroadcastReceiver中提取数据之外的任何自定义方法都不会被调用。

我尝试将自定义方法移至另一个类并创建该类的实例来调用这些方法。.但是没有运气

         @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction() == SmsReceived)
        {
            Bundle bundle = intent.getExtras();
            if(bundle!=null)
            {
                Object[] mypdu = (Object[])bundle.get("pdus");
                final SmsMessage[] message = new SmsMessage[mypdu.length];
                for(int i=0;i<mypdu.length;i++) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        String format = bundle.getString("format");
                        message[i] = SmsMessage.createFromPdu((byte[]) 
  mypdu[i], format);
                    } else {
                        message[i] = SmsMessage.createFromPdu((byte[]) 
   mypdu[i]);
                    }

                    msg = message[i].getMessageBody();
                    phone = message[i].getOriginatingAddress();
              /*phone =phone.replace("+","0");
                    phone = "0" + phone ;*/
                    String newPhone = "00";
                    for (int j = 1; j < phone.length(); j++) {
                        newPhone += String.valueOf(phone.charAt(j));
                    }

                    Log.v("number", phone);


                    Toast.makeText(context, "message : " + msg + " " + 
         newPhone, Toast.LENGTH_LONG).show();
 }}}}

我想在帖子中获得该号码为“ 008801 ...”,但它总是打印“ +8801 ....”,这是从BroadcastReceiver中提取的

0 个答案:

没有答案