我有一个小应用程序,我可以在其中获取我的位置或手动设置它。设置接近警报。一旦我进入定义的区域(某些半径等),接近警报接收器就会显示 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;
}
}
日志猫:
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