开发者

How do I play a sound when my application receives a sms?

I have used BroadcastReceiver class in my application to receive incoming sms and also give the high preference to my application in manifest file so that incoming message should be handled by my application first.

My main file is:

package com.example.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;



public class Demo extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) 
{
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String str = "";            
    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
            //   str += "SMS from " + msgs[i].getOriginatingAddress();                     
            //  str += " :";
            str += msgs[i].getMessageBody().toString();
            //   str += "\n";        
        }
        //---display the new SMS message---
        //Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        String[] msgText;
        String newstr="";
        msgText=str.split(",");
        newstr+=msgText[1]+","+msgText[2];
        if(msgText[0].equals("button"))
        {                
            Intent i = new Intent(context,Second.class);
            i.putExtra("msg",newstr);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
            context.startActivity(i);
        }
    }
    this.abortBroadcast();
}



}

And manifest file is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name="Second" class=".second"
        android:label="Demo">
    </activity>
    <receiver android:name=".Demo">
        <intent-filter android:priority="100">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
</manifest>

What I want to know is how can I play a sound when my application receives a sms?

I am confused about where should I implement the code because after analyzing the data I am switc开发者_StackOverflowhing to the second screen (if msg is in a particular format).


You can use the NotificationManager to play a sound when you receive an SMS. Just place the sound file in res/raw.

NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();         
notification.sound = Uri.parse("android.resource://com.your.package/raw/sound_file");
nm.notify(0, notification);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜