我正在尝试使用receiver读取传入的消息。我按照教程并添加了下面的代码,但是广播接收器没有被激活。放在onReceive中的日志永远不会被解雇。
清单
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:name=".xyz"
android:allowBackup="true"
android:icon="@mipmap/icon_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name="xyz.receiver.SmsReceiver">
<intent-filter android:priority="1">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
BroadcastReceiver
public class SmsReceiver extends BroadcastReceiver {
private static SmsListener mListener;
@Override
public void onReceive(Context context, Intent intent) {
Bundle data = intent.getExtras();
Log.e("TEST",data.toString());
Object[] pdus = (Object[]) data.get("pdus");
for(int i=0;i<pdus.length;i++){
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
String sender = smsMessage.getDisplayOriginatingAddress();
String messageBody = smsMessage.getMessageBody();
mListener.messageReceived(messageBody);
}
}
public static void bindListener(SmsListener listener) {
mListener = listener;
}
}
活动
SmsReceiver.bindListener(new SmsListener() {
@Override
public void messageReceived(String messageText) {
Toast.makeText(OTPActivity.this, "Message: " + messageText, Toast.LENGTH_LONG).show();
}
});
答案 0 :(得分:0)
我已经使用过此代码,希望这也适用于你:)
private BroadcastReceiver mSMSReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
try {
Log.e("sms receiver", "0000sdfghjkjhgfdsdfghjk");
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
String sms;
for (Object pdu : pdus) {
SmsMessage tmp;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
tmp = SmsMessage.createFromPdu((byte[]) pdu, bundle.getString("format"));
} else {
tmp = SmsMessage.createFromPdu((byte[]) pdu);
}
String senderMobile = tmp.getMessageBody();
sms = tmp.getMessageBody();
if (senderMobile.startsWith("کد فعالسازی شما در توپ 360:")) {
Log.e("sms receiver", "1111sdfghjkjhgfdsdfghjk");
final String mSMS = sms.replaceAll("[^0-9]", "");
//TODO Check SMS Center number with senderMobile if(senderMobile.equals(989100000)){...}
if (mSMS.length() == 5 || mSMS.length() == 6) {
Log.e("sms receiver", "2222sdfghjkjhgfdsdfghjk");
runOnUiThread(new Runnable() {
@Override
public void run() {
//CODE IS mSMS
code.setText(mSMS);
Log.e("sms receiver", "3333sdfghjkjhgfdsdfghjk");
}
});
abortBroadcast();
break;
}
}
}
}
}
} catch (Exception e) {
Log.e("sms exception", e.getMessage());
}
}
};
@Override
public void onResume() {
super.onResume();
if (isGranted) {
IntentFilter mIntentFilter = new IntentFilter();
Log.e("sms receiver", "4444sdfghjkjhgfdsdfghjk");
mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
mIntentFilter.setPriority(Integer.MAX_VALUE);
registerReceiver(mSMSReceiver, mIntentFilter);
} else {
Log.e("sms receiver", "5555sdfghjkjhgfdsdfghjk");
}
}
@Override
public void onPause() {
super.onPause();
if (isRegistered) {
unregisterReceiver(mSMSReceiver);
Log.e("sms receiver", "6666sdfghjkjhgfdsdfghjk");
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 100: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
isGranted = true;
IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
mIntentFilter.setPriority(Integer.MAX_VALUE);
registerReceiver(mSMSReceiver, mIntentFilter);
Log.e("sms receiver", "7777sdfghjkjhgfdsdfghjk");
isRegistered = true;
onResume();
} else {
isGranted = false;
Snackbar snackbar = Snackbar.make(coordinatorLayout, "دریافت کننده خودکار پیامک غیرفعال است.", Snackbar.LENGTH_LONG);
snackbar.show();
}
return;
}
}
}