我正在开发一个基于闹钟/振动的应用程序,它会在指定时间前10分钟和指定时间前2分钟发出警报。
两个警报都设置为振动30秒,这在手机保持解锁状态时有效。
然而,当手机被锁定并且最后一个闹钟振动时,它只会振动5或6秒钟?
以前有没有人遇到过这个问题?
更新
当电话被锁定并说电话因振动而亮起但是一旦屏幕再次变暗,那就是振动停止时
代码:
public void startAlert(View view) {
if (timeAllowedOut.getText().toString().isEmpty()) {
Toast.makeText(this, "Error, text missing!", Toast.LENGTH_LONG).show();
} else {
removedDate = new Date();
timeRemovedText.setText(almacDateFormat.format(removedDate.getTime()));
time1 = String.valueOf(timeRemovedText);
time2 = String.valueOf(timeAllowedOut);
VisualAlert.calculate();
mTime1 = Calendar.getInstance();
mTime2 = Calendar.getInstance();
mTime1.setTime(firstAlarmTime);
mTime2.setTime(secondAlarmTime);
//Set the 10 before to alert exactly 10 minutes before time
tenBefore =mTime1.getTimeInMillis();
//set 2 minutes before to not alert for seconds, therefore alerting at wall time.
twoBefore = trimSecsAndMillisecs(mTime2).getTimeInMillis();
removedTime = removedDate.getTime();
i = tenBefore != null ? tenBefore.intValue() : null;
j = twoBefore != null ? twoBefore.intValue() : null;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(VisualAlert.this);
//Set title
alertDialogBuilder.setTitle("Set alarm");
//set Dialog message
alertDialogBuilder
.setMessage("Desiccants removed at \n" + almacDateFormat.format(removedTime))
.setCancelable(false)
//if this button is click, set Alarm
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//set Alarm manager
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//Hide desiccant exposed until bottled clicked
btnExposed.setVisibility(View.GONE);
btnBotttled.setVisibility(View.VISIBLE);
// set both alerts if time allowed out greater than or equal to 10 minutes
if (removedDate.before(firstAlarmTime) || removedDate.equals(firstAlarmTime)) {
intent = new Intent(VisualAlert.this, MyAlarm.class);
intent1 = new Intent(VisualAlert.this, MyAlarm2.class);
//sending information to the broadcaster
intent.putExtra("extra", "exposed");
intent1.putExtra("extra", "exposed");
//set first alarm
pendingIntent = PendingIntent.getBroadcast(VisualAlert.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, tenBefore, pendingIntent);
//second alarm
pendingIntent2 = PendingIntent.getBroadcast(VisualAlert.this, j, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, twoBefore, pendingIntent2);
}
//only set the second alert if time less then 10 minutes
else {
intent1 = new Intent(VisualAlert.this, MyAlarm2.class);
intent1.putExtra("extra", "exposed");
pendingIntent2 = PendingIntent.getBroadcast(VisualAlert.this, j, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, twoBefore, pendingIntent2);
}
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if this button is click, do nothing
dialog.cancel();
}
});
//create alert Dialog
AlertDialog alertDialog = alertDialogBuilder.create();
//show it
alertDialog.show();
}
}
//Stop alarm, with all alert forms ending
public void stopAlert(View view) {
i = tenBefore !=null ? tenBefore.intValue() : null;
j = twoBefore !=null ? twoBefore.intValue() : null;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(VisualAlert.this);
returnedDate = new Date();
//Set title
alertDialogBuilder.setTitle("Desiccant bottled");
//set Dialog message
alertDialogBuilder
//Set alert dialog to display time desiccant was bottled.
.setMessage("Desiccants bottled at \n" + almacDateFormat.format(returnedDate.getTime()))
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Hide desiccant exposed until bottled clicked
btnExposed.setVisibility(View.GONE);
btnBotttled.setVisibility(View.GONE);
reset.setVisibility(View.VISIBLE);
//intent to class
intent = new Intent(VisualAlert.this, MyAlarm.class);
intent1 = new Intent(VisualAlert.this, MyAlarm2.class);
intent.putExtra("extra", "bottled");
intent1.putExtra("extra","bottled");
//pending intent to awake device
pendingIntent = PendingIntent.getBroadcast(VisualAlert.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent2 = PendingIntent.getBroadcast(VisualAlert.this, j, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.cancel(pendingIntent2);
sendBroadcast(intent);
sendBroadcast(intent1);
timeBottledText.setText(almacDateFormat.format(returnedDate.getTime()));
//change returnedDate and removedDated to Calendar
calendar = Calendar.getInstance();
cal = Calendar.getInstance();
calendar.setTime(returnedDate);
cal.setTime(removedDate);
//Calculate time elapsed (not accounting for seconds)
long diffInMilliSec = trimSecsAndMillisecs(calendar).getTimeInMillis() - trimSecsAndMillisecs(cal).getTimeInMillis();
long diffInMin = TimeUnit.MILLISECONDS.toMinutes(diffInMilliSec);
String total = String.valueOf(diffInMin);
if (returnedDate.before(removedDate)) {
totalTimeText.setText("--");
} else {
totalTimeText.setText(total);
}
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if this button is click, do nothing
dialog.cancel();
}
});
//create alert Dialog
AlertDialog alertDialog = alertDialogBuilder.create();
//show it
alertDialog.show();
}
//Update alert time when 'time allowed out' is changed
private TextWatcher mTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
//not needed (Method must be overridden)
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//not needed (Method must be overridden)
}
@Override
public void afterTextChanged(Editable view) {
calculate();
}
};
// Trims a Calendar object of its seconds and milliseconds
private static Calendar trimSecsAndMillisecs(Calendar cal) {
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.SECOND, 0);
return cal;
}
报警:
if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH));
//fetch the extra string values
String state = intent.getExtras().getString("extra");
Log.e("ringtone state:extra is", state);
//this converts the extra strings from the intent
//to start IDs, Values 0 and 1
assert state != null;
switch (state) {
case "exposed":
startId = 1;
break;
case "bottled":
startId = 0;
break;
default:
startId = 0;
break;
}
//if else statements
if (!this.isRunning && startId == 1) {
Log.e("if there was not sound1", " and you want to start");
//incorporate vibrating
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
//create as instance of the media player
mediaPlayer = MediaPlayer.create(this, R.raw.tick_tock_sound);
mediaPlayer.start();
mediaPlayer.setLooping(true);
v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
long[] pattern = {0, 100, 10};
v.vibrate(pattern, 0);
;
if (v.hasVibrator()) {
Log.v("Can Vibrate", "YES");
} else {
Log.v("Can Vibrate", "NO");
}
//turn flash light on
camera = Camera.open(0);
parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
createNotification(this, "Warning", "2 minutes to bottle Desiccants", "Alert");
this.isRunning = true;
this.startId = 0;
} else if (!this.isRunning && startId == 0) {
Log.e("if there was not sound", "and you want to end");
this.isRunning = false;
this.startId = 0;
} else if (!this.isRunning && startId == 1) {
Log.e("if there is sound", "and you want to start");
this.isRunning = true;
this.startId = 0;
} else {
Log.e("if there is sound", "and you want to end");
v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
//stop song playing
mediaPlayer.stop();
mediaPlayer.reset();
try {
releaseCameraAndPreview();
camera = Camera.open(1);
parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();
camera.release();
} catch (Exception e) {
Log.e(getString(R.string.app_name), "failed to open Camera");
e.printStackTrace();
}
//stop vibration
v.cancel();
this.isRunning = false;
this.startId = 0;
}
Log.e("My Activity", "In the service");
return START_NOT_STICKY;
}