2

我正在开发一个应用程序,它将在接收特定短信时播放指定的声音/歌曲(具有特定内容的短信说“测试”)。因此,如果用户手机接收到内容为“test”的消息,则必须播放声音。因此,我使用了Broadcastreceiver在接收 SMS 时激活并检查特定所需内容的 a。如果条件为真,我已经使用Soundpooland来播放声音。AudioManager

但是,不知何故,代码对我不起作用。如果可能的话,请看一下并纠正我。我想,我使用了错误的上下文或类似的......

SoundManager.java:

 package net.SMSMessaging;

import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class SoundManager 
 {
private  SoundPool mSoundPool; 
private  HashMap<Integer, Integer> mSoundPoolMap; 
private  AudioManager  mAudioManager;
private  Context mContext;

 public SoundManager()
{

}

public void initSounds(Context theContext) { 
     mContext = theContext;
     mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); 
     mSoundPoolMap = new HashMap<Integer, Integer>(); 
     mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);         
} 

public void addSound(int Index,int SoundID)                    
{
    mSoundPoolMap.put(1, mSoundPool.load(mContext, SoundID, 1)); 
}

public int playSound(int index) { 

     int streamVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
     int streamid = mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
     return streamid;
}

}

广播接收器文件 - SMSReceiver.java

package net.SMSMessaging;

import java.util.HashMap;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Button;
import android.widget.Toast;

 public class SMSReceiver extends BroadcastReceiver 
{    
private SoundManager mSoundManager;      
int id;      
Button btn;

@Override
public void onReceive(Context context, Intent intent) 
{
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String str = "";
    String str1 = ""; 

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 += " :";
            str1 += msgs[i].getMessageBody().toString();
           // str1 += "\n";        
        }       

        String str2= "test";
        if (str1.equals(str2))
        {
            Toast.makeText(context,"Matched... Performing operation",0).show();
            mSoundManager = new SoundManager();
            mSoundManager.initSounds(context);   // **This is where may be I m doing wrong. What should be the actual context to be passed??? I tried with getBaseContext() and it is not allowed.**
            mSoundManager.addSound(1, R.raw.sound);
            mSoundManager.playSound(1);

        }
    else
        {
            Toast.makeText(context,"Not matched",0).show();
        }
    }                

}
 }

所以,我把我认为我做错的地方标记在SMSReceiver.java. 我尝试用getBaseContext() 替换上下文,getApplicationContext()甚至用this. 但是,没有一个工作得很好。使用“上下文”不会导致应用程序崩溃,甚至连声音都不会播放。所以,我认为我需要使用不同的上下文。

如果您需要任何详细信息,请告诉我。

4

0 回答 0