接近警报如何触发音频文件

时间:2013-04-29 19:06:30

标签: android audio broadcastreceiver

我有一个小应用程序,我可以获取我的位置或手动设置它。设置接近警报。一旦我进入定义区域(特定半径等),接近警报接收器就会显示Toast消息。一切正常,但是......我也希望onReceive方法播放短音和振动。不确定是否可能,所以任何帮助都值得赞赏。提前致谢。以下代码崩溃,错误接收广播意图blah-blah(有额外的)... 这是我的Proximity接收器类:

package com.reminder;
public class ProximityReceiver extends BroadcastReceiver 
{

  private static final Context Context = null;

@Override
 public void onReceive(Context arg0, Intent arg1)
 {
      // TODO Auto-generated method stub
      String action = LocationManager.KEY_PROXIMITY_ENTERING;    // To deifine if user is leaving or entering 
      boolean state = arg1.getBooleanExtra(action, false);
      if(state)
  {
      // When entering area
    try {
        playBeep();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  


      Toast.makeText(arg0, "You have entered your area", Toast.LENGTH_LONG).show();

  }
  else
  {
      //When leaving area
      Toast.makeText(arg0, "You are leaving your area", Toast.LENGTH_LONG).show();
  }
}

  public void playBeep() throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {
        try
        {
             Uri myUri = Uri.parse("android.resource://com.reminder/res/" + R.raw.beep); // initialize Uri here
                MediaPlayer mediaPlayer = new MediaPlayer();
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mediaPlayer.setDataSource(getApplicationContext(), myUri);
                mediaPlayer.prepare();
                mediaPlayer.start();
        }
        catch (NullPointerException e)
        {
            Toast.makeText( Context, "exeption: "+e, Toast.LENGTH_LONG).show();

        };

    }

private Context getApplicationContext() {
    // TODO Auto-generated method stub
    return null;
}

}

logcat的:

   04-29 20:28:42.925: D/ActivityThread(6597): BDC-Calling onReceive: intent=Intent { act=com.paad.trasurealert flg=0x10 (has extras) }, ordered=true,  receiver=com.reminder.ProximityReceiver@415d2568, IIntentReceiver=415c5630
  04-29 20:28:42.928: D/ActivityThread(6597): BDC-Calling finishReceiver: IIntentReceiver=415c5630
  04-29 20:28:42.928: D/AndroidRuntime(6597): Shutting down VM
  04-29 20:28:42.928: W/dalvikvm(6597): threadid=1: thread exiting with uncaught exception (group=0x40cbd258)
  04-29 20:28:42.930: E/AndroidRuntime(6597): FATAL EXCEPTION: main
  04-29 20:28:42.930: E/AndroidRuntime(6597): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.paad.trasurealert flg=0x10 (has extras) } in com.reminder.ProximityReceiver@415d2568
  04-29 20:28:42.930: E/AndroidRuntime(6597):   at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:755)
  04-29 20:28:42.930: E/AndroidRuntime(6597):   at android.os.Handler.handleCallback(Handler.java:605)
  04-29 20:28:42.930: E/AndroidRuntime(6597):   at android.os.Handler.dispatchMessage(Handler.java:92)
  04-29 20:28:42.930: E/AndroidRuntime(6597):   at android.os.Looper.loop(Looper.java:154)
  04-29 20:28:42.930: E/AndroidRuntime(6597):   at android.app.ActivityThread.main(ActivityThread.java:4624)
  04-29 20:28:42.930: E/AndroidRuntime(6597):   at java.lang.reflect.Method.invokeNative(Native Method)
  04-29 20:28:42.930: E/AndroidRuntime(6597):   at java.lang.reflect.Method.invoke(Method.java:511)
  04-29 20:28:42.930: E/AndroidRuntime(6597):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
  04-29 20:28:42.930: E/AndroidRuntime(6597):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
  04-29 20:28:42.930: E/AndroidRuntime(6597):   at dalvik.system.NativeStart.main(Native Method)
  04-29 20:28:42.930: E/AndroidRuntime(6597): Caused by: java.lang.NullPointerException
  04-29 20:28:42.930: E/AndroidRuntime(6597):   at android.widget.Toast.<init>(Toast.java:92)
  04-29 20:28:42.930: E/AndroidRuntime(6597):   at android.widget.Toast.makeText(Toast.java:233)
  04-29 20:28:42.930: E/AndroidRuntime(6597):   at com.reminder.ProximityReceiver.playBeep(ProximityReceiver.java:70)
  04-29 20:28:42.930: E/AndroidRuntime(6597):   at com.reminder.ProximityReceiver.onReceive(ProximityReceiver.java:32)
  04-29 20:28:42.930: E/AndroidRuntime(6597):   at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:746)
  04-29 20:28:42.930: E/AndroidRuntime(6597):   ... 9 more

1 个答案:

答案 0 :(得分:0)

在您的playBeep()方法中,您有这一行......

mediaPlayer.setDataSource(getApplicationContext(), myUri);

...但您的getApplicationContext()方法会返回null ...

private Context getApplicationContext() {
    // TODO Auto-generated method stub
    return null;
}

摆脱那种方法。您已经获得Contextarg0参数作为onReceive(...)的{​​{1}}参数。将您的playBeep()方法更改为......

public void playBeep(Context context)

...然后在onReceive(...)中使用...

调用它
playBeep(arg0);

...在方法本身中,调用...

mediaPlayer.setDataSource(context, myUri);

同样在该方法的catch块中,这是错误的......

Toast.makeText( Context, "exeption: "+e, Toast.LENGTH_LONG).show();

...将Context(大写'C')更改为context(小写'c')。