Android彩信接收器无法正常工作

时间:2013-01-08 21:09:47

标签: android broadcastreceiver

我已经在这个问题上敲了几天,现在决定我会请求一些帮助。我正在尝试让我的Android应用程序响应收到的mms消息。以下是我的主要内容:

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”。此代码适用于短信,因此它必须是mms特有的。这是我第一次在这里发帖,所以如果我错过了什么,请告诉我。

2 个答案:

答案 0 :(得分:0)

您是否在AndroidManifest.xml中声明了它们? 您是否向Manifest添加了所需的权限?

<uses-permission android:name="android.permission.RECEIVE_MMS" />

答案 1 :(得分: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());
            }
        }  
    }