Android 应用程序在几声蜂鸣声后意外停止
我为一个应用程序编写了一个代码,当手机倾斜一定角度时,它会发出嘟嘟声。它在一开始工作正常,但当我把手机倾斜到第七或第八次以后,它就不再发出哔哔声了。当我检查应用程序状态时,它显示为正在运行。我的服务代码是Android 应用程序在几声蜂鸣声后意外停止,android,Android,我为一个应用程序编写了一个代码,当手机倾斜一定角度时,它会发出嘟嘟声。它在一开始工作正常,但当我把手机倾斜到第七或第八次以后,它就不再发出哔哔声了。当我检查应用程序状态时,它显示为正在运行。我的服务代码是 public class TheService extends Service implements SensorEventListener { public static final String TAG = TheService.class.getName(); publi
public class TheService extends Service implements SensorEventListener {
public static final String TAG = TheService.class.getName();
public static final int SCREEN_OFF_RECEIVER_DELAY = 500;
private SensorManager mSensorManager = null;
private WakeLock mWakeLock = null;
private float lastY;
MediaPlayer mp1;
private MediaPlayer mMediaPlayer;
/*
* Register this as a sensor event listener.
*/
private void registerListener() {
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
/*
* Un-register this as a sensor event listener.
*/
private void unregisterListener() {
mSensorManager.unregisterListener(this);
}
public BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive("+intent+")");
if (!intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
return;
}
Runnable runnable = new Runnable() {
public void run() {
Log.i(TAG, "Runnable executing.");
unregisterListener();
registerListener();
}
};
new Handler().postDelayed(runnable, SCREEN_OFF_RECEIVER_DELAY);
}
};
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.i(TAG, "onAccuracyChanged().");
}
public void onSensorChanged(SensorEvent event) {
Log.i(TAG, "onSensorChanged().");
lastY = event.values[2];
if (lastY <9 && lastY > 8) {
mMediaPlayer = MediaPlayer.create(this, R.raw.help);
mMediaPlayer.start();
}
}
@Override
public void onCreate() {
super.onCreate();
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
PowerManager manager =
(PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
}
@Override
public void onDestroy() {
mMediaPlayer.release();
unregisterReceiver(mReceiver);
unregisterListener();
mWakeLock.release();
stopForeground(true);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startForeground(Process.myPid(), new Notification());
registerListener();
mWakeLock.acquire();
return START_STICKY;
}
}
我不确定安卓是否正在扼杀它,或者我的代码中是否存在任何问题。如果安卓一直这么做,该应用程序就不会显示为在后台运行。请帮忙。您有日志吗
避免重新创建MediaPlayer:
if(mMediaPlayer == null)
mMediaPlayer = MediaPlayer.create(this, R.raw.help);
我不知道如何检查!!你能告诉我它现在工作正常吗。非常感谢,伊切沃。再问一个问题;因为我正在使用wakelock,所以即使手机处于待机状态,我的应用程序也会发出嘟嘟声,这会耗尽电池电量吗?是的,它会耗尽电池电量。从-始终确保禁用不需要的传感器,尤其是在活动暂停时。否则可能会在几个小时内耗尽电池电量。请注意,当屏幕关闭时,系统不会自动禁用传感器。
if(mMediaPlayer == null)
mMediaPlayer = MediaPlayer.create(this, R.raw.help);