在手机号码 onReceive()中传入的消息无效后。请帮我找到解决此问题的方法。 这是我的代码,如果我错了,请检查并纠正我。
public class IncomingSms extends BroadcastReceiver {
Context con;
@Override
public void onReceive(Context context, Intent intent) {
this.con = context;
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
Log.e("IncomingSms", "onReceive..");
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
for (String key : bundle.keySet())
Log.e("okkk", key + "// " + bundle.getString(key));
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
try {
if (senderNum.equals("TA-DOCOMO")) {
CodeVerificationActivity Sms = new CodeVerificationActivity();
Sms.recivedSms(message);
}
} catch (Exception e) {
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
这个我的清单文件代码used.please告诉我正确的方法,如果我错过了代码中的东西然后纠正我。实际上我正在读取传入的消息在android中自动验证OTP。这对我来说非常重要。提前致谢
<receiver android:name=".Util.IncomingSms"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
答案 0 :(得分:1)
你在检查棉花糖吗?然后允许设置应用程序的权限。
或试试这个。
public class SMSBroadcast extends BroadcastReceiver {
final SmsManager manager = SmsManager.getDefault();
@Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdus = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage currentmessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
String phoneNumber = currentmessage.getDisplayOriginatingAddress();
String message = currentmessage.getDisplayMessageBody();
Toast.makeText(context, "number " + phoneNumber + " message :" + message, Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
的Manifest.xml
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name="com.inficare.avinashverma.googlecloudmessagingdemo.broadcast.SMSBroadcast">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 1 :(得分:1)
好的,首先像这样更改你的代码
public class IncomingSms extends BroadcastReceiver {
public static final String SMS_BUNDLE = "pdus";
String address;
SmsMessage smsMessage;
public void onReceive(Context context, Intent intent) {
Bundle intentExtras = intent.getExtras();
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
for (int i = 0; i < sms.length; ++i) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String format = intentExtras.getString("format");
smsMessage = SmsMessage.createFromPdu((byte[]) sms[i], format);
}
else {
smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
}
// SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
address = smsMessage.getOriginatingAddress();
}
// Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
//this will update the UI with message
YourActivity inst = Youractivity.instance();//write your activity
// where you are checking for message.
inst.updateList(address);
}
}
}
然后在你的活动课程内
public static YourActivity instance() {
return inst;
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
public void updateList(final String smsMessage) {
if (smsMessage.equalsIgnoreCase("IM-DOCOMO")) {
Toast.makeText(VerifyNumber.this, "Successfully verified",
Toast.LENGTH_LONG).show();
}
}
Then in onresume and onpause register your receiver so that you need to add
your receiver in manifest
public void onResume(){
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
try{
registerReceiver(broadCastReceiver, filter);
}catch(IllegalArgumentException e){
e.printStackTrace();
}
}
public void onPause()
{
super.onPause();
try {
unregisterReceiver(broadCastReceiver);
} catch(IllegalArgumentException e){
e.printStackTrace();
}
}