如何传递广播接收者上下文?

发布于 2024-12-22 06:20:06 字数 3678 浏览 1 评论 0原文

我正在开发一个应用程序,它将在收到特定短信(具有特定内容的短信说“测试”)时播放指定的声音/歌曲。因此,如果用户手机收到内容为“test”的消息,则必须播放声音。因此,我使用了 Broadcastreceiver ,它在接收短信时变得活跃并检查特定的所需内容。我已经使用 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;
}

}

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

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

I am developing an application which will play a specified sound/song on receiving a specific sms (sms with specific content say "test"). Thus, if user phone receives a message with content "test", a sound must be played. So, I have used a Broadcastreceiver which becomes active on receiving SMS and checks for specific required content. I have used Soundpool and AudioManager for playing sound if condition becomes true.

But, somehow, the code is not working for me. Please take a look and correct me, if possible. I guess, I am using wrong context or such...

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 file - 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();
        }
    }                

}
 }

So, I have marked the place where I think I am doing wrong in SMSReceiver.java. I have tried replacing the context with getBaseContext(), getApplicationContext() and even this. But, none is working well. The use of "context" is not crashing the application, but even the sound is not played. So, I think I need to use a different context.

Please let me know if you need any detail.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文