我正在尝试在我的程序中收到彩信。以下是我的清单中的内容:
<receiver android:name="com.webnation.text2email.receivers.MMSBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
然而,当我收到彩信时,我的广播接收器在调试器中单步执行时从不被调用。我在Galaxy 7(API 24)上运行Android 7.0。他们是否以编程方式停止接收彩信?
这是我的广播接收器。这一切都曾经奏效。
public class MMSBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
this.context = context;
String action = intent.getAction();
String type = intent.getType();
int typeMessage = -1;
byte[] pushData;
GenericPdu pdu = null;
if (action.equals(ACTION_MMS_RECEIVED) && type.equals(MMS_DATA_TYPE)) {
Bundle bundle = intent.getExtras();
Timber.d("bundle " + bundle);
if (bundle != null) {
pushData = intent.getByteArrayExtra("data");
PduParser parser = new PduParser(pushData);
pdu = parser.parse();
PduHeaders headers = pdu.getPduHeaders();
Timber.d("buffer " + pushData);
String incomingNumber = new String(pushData);
int indx = incomingNumber.indexOf("/TYPE"); // not used, parse the number in MMUtils.getAddress instead
if (indx > 0 && (indx - 15) > 0) {
int newIndx = indx - 15;
incomingNumber = incomingNumber.substring(newIndx, indx);
char[] characters = incomingNumber.toCharArray();
int index = 0;
for (int i=0;i<characters.length;i++) {
if (Character.isDigit(characters[i]) ){
index = i;
break;
}
}
//indx = incomingNumber.indexOf("+");
if (index > 0) {
incomingNumber = incomingNumber.substring(index);
TelephonyManager tm = (TelephonyManager)context.getSystemService(context.getApplicationContext().TELEPHONY_SERVICE);
String countryCode = tm.getNetworkCountryIso().toUpperCase();
try {
String prefix = CountryToPhonePrefix.prefixFor(countryCode).replace("+", "");
int indexOfBaseNumber = incomingNumber.indexOf(prefix);
if (indexOfBaseNumber > -1) {
if (indexOfBaseNumber == 0) {
int lengthPrefix = prefix.length();
indexOfBaseNumber = indexOfBaseNumber + lengthPrefix;
}
incomingNumber = incomingNumber.substring(indexOfBaseNumber);
}
} catch (IllegalArgumentException ie) {
Timber.e(ie);
} catch (IndexOutOfBoundsException ie) {
Timber.e(ie);;
}
Timber.d("Mobile Number: " + incomingNumber);
}
}
int transactionId = bundle.getInt("transactionId", -1);
Timber.d("transactionId " + transactionId);
int pduType = bundle.getInt("pduType", -1);
Timber.d("pduType " + pduType);
byte[] buffer2 = bundle.getByteArray("header");
if (buffer2 != null) {
String header = new String(buffer2);
Timber.d("header " + header);
}
List<MMSMessage> textMessages = MMSUtils.getMessagesFrom(context, intent); //meat of onReceive()
sendMessages(textMessages);
} else {
Timber.e("Invalid PUSH data");
}
}
}
}
编辑:添加RECEIVE_WAP_PUSH权限后,应用程序开始在调试器中工作,正确接收和处理彩信。但是,在调试模式下,应用程序不处理MMS消息。
我尝试在我的接收器中使用android.provider.Telephony.WAP_PUSH_DELIVER操作,有人指出这是不正确的,如果不是默认的应用程序。