用于接收的短信的广播接收器不会在所有手机上激活

时间:2015-12-29 11:32:17

标签: android sms broadcast receiver

我正在制作一个应用程序,它利用广播接收器获取手机中收到的特定短信息。广播接收器要获取的短信息包含“!test!”在它们中,然后通过HTTP帖子发送到服务器。 此程序在某些手机上正常运行,而在其他手机上则(虽然有时会这样)。

问题在于广播接收器未被激活。根据我最近进行的研究,当具有较高优先级的广播接收器首先获得sms广播然后中止它,阻止它被传送到其他电话时,似乎会出现这个问题。 一些解决方案提供了将intent filter的优先级设置为999或1000的位置。但是这并没有最终解决问题

此外,我已经阅读了从Android 4.4及以上版本中止播放是不允许的,虽然我似乎仍然面临着类似的Android版本问题。

尽管我知道这与Android操作系统存在的一些故障本质上更相关,但为方便起见,我提供了一些代码片段。 我想知道是否有某种方法可以解决这个问题。如果没有,那么需要考虑的建议是什么?

在on create方法中以编程方式注册接收器。

registerReceiver(smsreceiver, new    IntentFilter("android.provider.Telephony.SMS_RECEIVED"));

收到程序:

 private BroadcastReceiver smsreceiver = new BroadcastReceiver()
{
    @Override


    public void onReceive(Context context, Intent intent)
    {
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;

        String phonenumber = " ";
        String msg = " ";

        if(null != bundle)
        {
            String info = "Text SMS from ";
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            String info1 = "";
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                info += msgs[i].getOriginatingAddress();  
                phonenumber = msgs[i].getOriginatingAddress();  
                info += "\n*****TEXT MESSAGE*****\n";
                info += msgs[i].getMessageBody().toString();
                //msg = msgs[i].getMessageBody().toString();

                msg += msgs[i].getMessageBody().toString();

                info1 = "";
                if (msgs[i].getServiceCenterAddress()!=null)
               // info1 = info1 + " Service Center:" + msgs[i].getServiceCenterAddress().toString();


               //  info1 = info1 + " Protocol identifier:" + msgs[i].getProtocolIdentifier();
               //  info1 = info1 + " Time stamp in milisec:" + msgs[i].getTimestampMillis();


                // info1 = info1 + "=" + msgs[i].getServiceCenterAddress().toString();

                 info1 =  msgs[i].getServiceCenterAddress().toString();

                 info1 = info1 + "=" + msgs[i].getProtocolIdentifier();
                 info1 = info1 + "=" + msgs[i].getTimestampMillis();

                 long milliSeconds = msgs[i].getTimestampMillis();
                 // milliSeconds is the time of real message it is datestam in miliseconds 



                 Calendar calendar = Calendar.getInstance();
                 // this shoule be the DateTime on moible when the message is recived 
                 //long timeNow= System.currentTimeMillis();

               //  Long tsLong = System.currentTimeMillis()/1000;
                 Long tsLong = System.currentTimeMillis();
                 String ts = tsLong.toString();

                 //calendar.getTimeInMillis().setTimeInMillis(milliSeconds);

                 //int mYear = calendar.get(Calendar.YEAR);
                 //int mMonth = calendar.get(Calendar.MONTH);
                 //int mDay = calendar.get(Calendar.DAY_OF_MONTH);

                // int mHour = calendar.get(Calendar.HOUR);
                // int mMin = calendar.get(Calendar.MINUTE);
                // int mSec = calendar.get(Calendar.SECOND);

                 //2015-09-11 23:09:10
                 // Y-  m-  d      H: i: s

                // String totalDateTime = Integer.toString(mYear) + "-" + Integer.toString(mMonth)+ "-"+ Integer.toString(mDay) + " " + Integer.toString(mHour) + ":" + Integer.toString(mMin)+ ":"+ Integer.toString(mSec);
                 String totalDateTime = ts; // this should be the time from the phone when SMS is received

                 info1 = info1 + "=" + totalDateTime;

            }

            // HERE make the new comment next line LINE COMMNET 
           //    Toast.makeText(context, info, Toast.LENGTH_SHORT).show();
            info = phonenumber + "_"+ msg;
           // msg = msg.toLowerCase();
            if (msg.contains("!test!")==true)
           // {
           //  Toast.makeText(context, "location in msg", Toast.LENGTH_SHORT).show();
           // }
           // else
            {
                // here to o http to web php to save the data
              Toast.makeText(context, info, Toast.LENGTH_SHORT).show();
              txtName = (TextView) findViewById(R.id.name);
              String phonenumberRec = txtName.getText().toString();


              String codeofsender = msg.substring(6, 11); 

              String all = phonenumberRec + "|"+msg+"|"+phonenumber+"|"+codeofsender+"|"+info1;


              ConnectivityManager cm =
                        (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

                NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
                boolean isConnected = activeNetwork != null &&
                                      activeNetwork.isConnectedOrConnecting();




                //StringEntity allF = new StringEntity(all, "UTF-8");
                //

                String _data = GetDateTimeWithProcedure("In SMS before network connected ");
                WriteToLogTextFile(_data);

                if (isConnected)
                {
                    String _data1 = GetDateTimeWithProcedure("In SMS network connected ");

                    if (activeNetwork != null && activeNetwork.isConnected() && activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
                    {
                        _data1 = "WIFI " + _data1;       
                    }  

                    if (activeNetwork != null && activeNetwork.isConnected() && activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                    {
                        _data1 = "Mobile Network " + _data1;       
                    } 

                    WriteToLogTextFile2(_data1);

                     new MyAsyncTask().execute(all);
                }


             // String res = postDataToWebServer(phonenumberRec,msg,phonenumber,codeofsender, info1);

            }

        }                         
    }
};

在android manfiest.xml中定义接收器:

 <receiver android:name=".smsreceiver">
        <intent-filter android:priority="999">           
 </intent-filter>

1 个答案:

答案 0 :(得分:0)

将清单更改为

<receiver
    android:name=".YourSMSReceiverClass"
    android:exported="true">
    <intent-filter android:priority="999">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>