1

几天来,我一直在努力解决这个问题,并决定寻求帮助。我正在尝试让我的 Android 应用程序响应收到的彩信。这是我的主要内容:

private IntentFilter filter = new IntentFilter(MMSReceiver.MMS_RECEIVED);
private MMSReceiver mmsReceiver = new MMSReceiver();
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ...

    registerReceiver(mmsReceiver, filter);

    ...
}

我还有一个 MMSReceiver 类:

public class MMSReceiver extends BroadcastReceiver {

public static final String MMS_RECEIVED = "android.provider.Telephony.MMS_RECEIVED";

public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(MMS_RECEIVED)){
        System.out.println("b:mms received");
    }
}

在看到另一个问题的建议后,我还尝试制作字符串“android.provider.Telephony.WAP_PUSH_RECEIVED”。此代码适用于 sms,因此它必须是特定于 mms 的。这是我第一次在这里发帖,所以如果我遗漏了什么,请告诉我。

4

2 回答 2

0

您是否在 AndroidManifest.xml 中声明了它们?您是否将所需的权限添加到清单?

<uses-permission android:name="android.permission.RECEIVE_MMS" />
于 2013-01-08T21:15:51.493 回答
0

这对我有用:

在 onCreate() 处:

try {
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.provider.Telephony.WAP_PUSH_RECEIVED");
    filter.addDataType("application/vnd.wap.mms-message");
    receivedMMSListener = new ReceivedMMSListener();
    registerReceiver(receivedMMSListener, filter);
} catch(Exception e){
    Log.d(TAG, "----> ERROR ADDING MMS FILTER: " + e.getLocalizedMessage());
}

和广播接收器:

    public class ReceivedMMSListener extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            /*
            "transactionId" - (Integer) The WAP transaction ID
            "pduType" - (Integer) The WAP PDU type
            "header" - (byte[]) The header of the message
            "data" - (byte[]) The data payload of the message
            */

            try {
                Bundle bundle = intent.getExtras();
                Integer transactionId = bundle.getInt("transactionId");
                Integer pduType = bundle.getInt("pduType");
                byte[] header = bundle.getByteArray("header");
                byte[] data = bundle.getByteArray("data");

                Log.d("TAG", "---> MMS | transactionId: " + transactionId.toString());
                Log.d("TAG", "---> MMS | pduType: " + pduType.toString());
                Log.d("TAG", "---> MMS | header: " + header.toString());
                Log.d("TAG", "---> MMS | data: " + data.toString());

            } catch(Exception e){
                Log.d(TAG, "---> ERROR EXTRACTING MMS: " + e.getLocalizedMessage());
            }
        }  
    }
于 2020-03-31T14:36:45.590 回答