我有一个检测噪音水平的应用程序,当在后台运行时噪音水平过高时,它会发出通知。当我单击通知时,它会将我重定向到“活动”,但是录音功能已停止。当我单击通知后尝试从活动中再次启动声音计时,它崩溃了。听和停止按钮不起作用。这就是我所拥有的:
public void tripAlarm(double Db){
alertActive = true;
mRecorder.pause();
if (Build.VERSION.SDK_INT >= 26) {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE));
}else {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(150);
}
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(false);
builder.setTitle("Sound Levels Are Too High");
builder.setMessage((df.format(Db)) + " dB");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// alertTextView.setVisibility(View.VISIBLE);
alertActive = false;
mRecorder.resume();
}
});
builder.show();
//Show the notification if the noise levels get too loud in the foreground
String title = "Sound Meter";
String message = "Sound levels are too high";
//Created an Intent for Main Activity
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(this,CHANNEL_ID)
.setSmallIcon(R.drawable.ic_android)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setContentIntent(pendingIntent)
.build();
notificationManager.notify(1,notification);
}
单击通知后,我希望它可以带我回到mainActivity,并且录音功能像以前一样正常工作。