Android 未在所有手机上激活接收到的sms消息的广播接收器
我正在制作一个应用程序,它利用广播接收器获取手机接收到的特定短信。广播接收器获取的sms消息中包含“!test!”,然后通过HTTP post发送到服务器此过程在某些手机上正常运行,,而在其他手机上则不正常(尽管有时会) 问题在于广播接收器未激活。根据我最近进行的研究,当具有较高优先级的广播接收器首先获得sms广播,然后中止该广播,从而阻止其发送到其他手机时,似乎会出现此问题提供了一些解决方案,可将intent filter的优先级设置为999或1000。然而,这并没有最终解决问题 此外,我还从安卓版本4.4及以上版本中了解到,中止广播是不允许的,尽管我似乎在这类安卓版本上也面临着类似的问题 尽管我知道这本质上与安卓操作系统存在的一些故障更相关,但为了方便起见,我提供了一些代码片段我想知道是否有一些方法可以解决这样的问题。如果没有,有哪些建议值得考虑? 在on create方法中按语法注册接收器Android 未在所有手机上激活接收到的sms消息的广播接收器,android,sms,broadcast,receiver,Android,Sms,Broadcast,Receiver,我正在制作一个应用程序,它利用广播接收器获取手机接收到的特定短信。广播接收器获取的sms消息中包含“!test!”,然后通过HTTP post发送到服务器此过程在某些手机上正常运行,,而在其他手机上则不正常(尽管有时会) 问题在于广播接收器未激活。根据我最近进行的研究,当具有较高优先级的广播接收器首先获得sms广播,然后中止该广播,从而阻止其发送到其他手机时,似乎会出现此问题提供了一些解决方案,可将intent filter的优先级设置为999或1000。然而,这并没有最终解决问题 此外,我还从
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);
}
}
}
};
private BroadcastReceiver smsreceiver=new BroadcastReceiver()
{
@凌驾
公共void onReceive(上下文、意图)
{
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
字符串phonenumber=“”;
字符串msg=“”;
如果(null!=捆绑)
{
String info=“文本短信来源”;
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];
字符串info1=“”;
对于(int i=0;i将清单更改为
<receiver
android:name=".YourSMSReceiverClass"
android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
您设置权限了吗?@TreyCai,是的,我设置了。,。如果我没有设置任何权限,这将根本不起作用。我在上面已经说过,这在我的手机和其他一些手机上正常工作。如果您在清单中静态注册收件人,则无需在代码中动态注册,尽管您不需要o修复
条目和相应的类。另外,您正确地认为在API>18中无法中止SMS_接收的
广播,因此我猜在接收时,如果在该场景中接收器出现故障,则接收器未在活动组件中正确注册。除此之外,您没有太多的错误在versions@MikeM中有一个do。感谢您提供的信息。我对接收者进行了统计注册,因为我不太清楚我可以动态添加优先级。因此,如果我要在统计上注册接收者,我需要应用哪些修复和更改?同时引用您提到的内容“如果接收器在该场景中出现故障,则在接收时未在活动组件中正确注册",如果是这种情况,它会适用于所有场景吗?就像我提到的那样,它在我的手机上运行得足够好。@MikeM。有一些手机已经测试过该应用程序,在一些手机上广播接收器工作正常,而其他手机则大部分是随机的。在随机的手机上,有时只有一个我们的广播接收器接收到一个数字外的消息。通过检查短信收件箱,所有消息似乎都是在应用程序运行时发送的。显然,如果我是正确的,它们似乎没有被另一个应用程序中止。原因可能是内存已满,Android操作系统随后必须释放一些内存空间通过删除不必要的进程?
<receiver
android:name=".YourSMSReceiverClass"
android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>