如何通过Broadcastreceiver上下文?

时间:2011-12-22 12:11:16

标签: android sms broadcastreceiver android-context soundpool

我正在开发一个应用程序,它将在接收特定短信时播放指定的声音/歌曲(具有特定内容的短信称为“test”)。因此,如果用户电话收到内容为“test”的消息,则必须播放声音。所以,我使用了Broadcastreceiver,它在接收SMS时变得活跃,并检查特定的所需内容。如果条件成立,我已使用SoundpoolAudioManager播放声音。

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

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;
}

}

Broadcastreceiver文件 - 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替换上下文。但是,没有一个运作良好。使用“上下文”不会使应用程序崩溃,但即使声音也没有播放。所以,我认为我需要使用不同的背景。

如果您需要任何细节,请告诉我。

0 个答案:

没有答案