Monitoring MMS_RECEIVED like SMS_RECEIVED on android

谁说胖子不能爱 提交于 2019-12-10 10:45:38

问题


So I'm setting up a program that plays a sound that is set when you receive a message, mms or sms. I got it to work with SMS but MMS doesn't do anything. Here is the code for the class that runs the BroadcastReceiver:

/**
 * This class overrides the main BroadcastReceiver to play the tone
 * at the given textSoundPath.
 * @author Jesse Stewart
 *
 */
public class TextMonitor extends BroadcastReceiver {

    public static String textSoundPath;     //this is the sound set to play when
                                            //sms or mms is received.

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        MediaPlayer tonePlayer = new MediaPlayer();

        try {
            tonePlayer.setDataSource(textSoundPath);
        } catch (Exception e) {
            System.out.println("Couldn't set the media player sound!!!!!");
            e.printStackTrace();
        }

        try {
            tonePlayer.prepare();
        } catch (Exception e) {
            System.out.println("Couldn't prepare the tone player!!!!!");
            e.printStackTrace();
        }

        tonePlayer.start();
    }

}

I set it up in the manifest like this:

<receiver android:name=".TextMonitor">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            <action android:name="android.provider.Telephony.MMS_RECEIVED" />
    </intent-filter>
</receiver>

and of course included:

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

I also tried doing the receiver in the manifest like this:

<receiver android:name=".TextMonitor">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

<receiver android:name=".TextMonitor">
    <intent-filter>
            <action android:name="android.provider.Telephony.MMS_RECEIVED" />
    </intent-filter>
</receiver>

I also tried putting in the receiver:

<action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED" />

But that just made nothing work.

Any help would be appreciated. Thanks. On a side note too, why do you put a period before the class name sometimes in the manifest but not others? like android:name=".TextMonitor" and then sometimes android:name="TextMonitor".


回答1:


You need to specify data scheme also.

Manifest entry should be

<receiver android:name=".PushReceiver">
  <intent-filter>
    <action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED" />
    <data android:mimeType="application/vnd.wap.mms-message" />
  </intent-filter>
</receiver>


来源:https://stackoverflow.com/questions/11289568/monitoring-mms-received-like-sms-received-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!