2

特定のSMSを受信したときに特定のサウンド/曲を再生するアプリケーションを開発しています(特定のコンテンツのSMSは「テスト」と言います)。したがって、ユーザーの電話が「test」という内容のメッセージを受信した場合、サウンドを再生する必要があります。Broadcastreceiverそのため、 SMS の受信時にアクティブになり、特定の必要なコンテンツをチェックする を使用しました。条件が真になった場合にサウンドを再生するためにSoundpoolとを使用しました。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;
}

}

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. しかし、どれもうまく機能していません。「コンテキスト」を使用してもアプリケーションがクラッシュすることはありませんが、サウンドも再生されません。したがって、別のコンテキストを使用する必要があると思います。

詳細が必要な場合はお知らせください。

4

0 に答える 0