您好,我请求您给我解决这个问题。 我在闹钟启动时创建了一个警报唤醒屏幕,它有两个按钮贪睡和解雇,它运行正常。 但问题是当用户按下手机上的后退按钮时,它会从屏幕上消失 声音还在播放。如果有人有解决方案,请提供给我。 谢谢
这是AlarmService类
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class AlarmService extends Service {
public static String TAG = AlarmService.class.getSimpleName();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent alarmIntent = new Intent(getBaseContext(), AlarmScreen.class);
alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try{
alarmIntent.putExtras(intent);
getApplication().startActivity(alarmIntent);
AlarmManagerHelper.setAlarms(this);
}catch(Exception e)
{
e.printStackTrace();
System.out.println(" May be null intent found if not snooz or dismiss the button by user on alarm screen");
}
return super.onStartCommand(intent, flags, startId);
}
}
这个是创建警报界面的接收器类
public class AlarmScreen extends Activity{
public final String TAG = this.getClass().getSimpleName();
private WakeLock mWakeLock;
private MediaPlayer mPlayer;
public static final String ID = "id";
public static final String NAME = "name";
public static final String TIME_HOUR = "timeHour";
public static final String TIME_MINUTE = "timeMinute";
public static final String TONE = "alarmTone";
public static final String NOTE = "note";
public static final String USER = "user";
static String name;
static String note;
static String userName;
static String tone;
static int timeHour;
static int timeMinute;
private static final int WAKELOCK_TIMEOUT = 60 * 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Setup layout
this.setContentView(R.layout.activity_alarm_screen);
/*parentView.getBackground().setAlpha(128);*/
name = getIntent().getStringExtra(AlarmManagerHelper.NAME);
note = getIntent().getStringExtra(AlarmManagerHelper.NOTE);
userName = getIntent().getStringExtra(AlarmManagerHelper.USER);
timeHour = getIntent().getIntExtra(AlarmManagerHelper.TIME_HOUR, 0);
timeMinute = getIntent().getIntExtra(AlarmManagerHelper.TIME_MINUTE, 0);
tone = getIntent().getStringExtra(AlarmManagerHelper.TONE);
TextView tvName = (TextView) findViewById(R.id.alarm_screen_name);
TextView tvNote = (TextView) findViewById(R.id.alarm_screen_note);
TextView tvUser = (TextView) findViewById(R.id.txt_nameUser);
tvName.setText(name);
tvNote.setText(note);
tvUser.setText(userName);
TextView tvTime = (TextView) findViewById(R.id.alarm_screen_time);
tvTime.setText(String.format("%02d : %02d", timeHour, timeMinute));
DisplayMetrics metrics = getResources().getDisplayMetrics();
final int screenWidth = (int) (metrics.widthPixels * 0.50);
final int screenHeight = (int) (metrics.heightPixels * 0.50);
// Get instance of Vibrator from current Context
final Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Start without a delay
// Each element then alternates between vibrate, sleep, vibrate, sleep...
long[] pattern = {0,800,200,1000,300,1000,200,4000};
if (v.hasVibrator()) {
Log.v("Can Vibrate", "YES");
} else {
Log.v("Can Vibrate", "NO");
}
Button dismissButton = (Button) findViewById(R.id.alarm_screen_button);
dismissButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mPlayer.stop();
v.cancel();
finish();
}
});
//adding snooz button
Button btn_snooz = (Button) findViewById(R.id.btn_snooz);
btn_snooz.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mPlayer.stop();
v.cancel();
sooz_activity();
System.out.println("Snooz Activity Started");
finish();
}
});
//Play alarm tone
mPlayer = new MediaPlayer();
try {
if (tone != null && !tone.equals("")) {
Uri toneUri = Uri.parse(tone);
if (toneUri != null) {
mPlayer.setDataSource(this, toneUri);
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mPlayer.setLooping(true);
mPlayer.prepare();
mPlayer.start();
v.vibrate(pattern, 0);
}
}
} catch (Exception e) {
e.printStackTrace();
}
//Ensure wakelock release
Runnable releaseWakelock = new Runnable() {
@Override
public void run() {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
// getWindow().setLayout(screenWidth, screenHeight);
/* getWindow().setFlags(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);*/
if (mWakeLock != null && mWakeLock.isHeld()) {
mWakeLock.release();
}
}
};
new Handler().postDelayed(releaseWakelock, WAKELOCK_TIMEOUT);
}
@SuppressWarnings("deprecation")
@Override
protected void onResume() {
super.onResume();
// Set the window to keep screen on
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
// getWindow().setLayout(screenWidth, screenHeight);
// Acquire wakelock
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
if (mWakeLock == null) {
mWakeLock = pm.newWakeLock((PowerManager.FULL_WAKE_LOCK | PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), TAG);
}
if (!mWakeLock.isHeld()) {
mWakeLock.acquire();
Log.i(TAG, "Wakelock aquired!!");
}
}
@Override
protected void onPause() {
super.onPause();
if (mWakeLock != null && mWakeLock.isHeld()) {
mWakeLock.release();
}
}
public void sooz_activity(){
PendingIntent pIntent = createPendingIntent(this);
long currentTimeMillis = System.currentTimeMillis();
long nextUpdateTimeMillis = currentTimeMillis + 5 * DateUtils.MINUTE_IN_MILLIS;
Time nextUpdateTime = new Time();
nextUpdateTime.set(nextUpdateTimeMillis);
setAlarm(this, nextUpdateTimeMillis, pIntent);
}
private static PendingIntent createPendingIntent(Context context) {
Intent intent = new Intent(context, AlarmService.class);
//intent.putExtra(ID, model.id);
intent.putExtra(NAME,name);
intent.putExtra(TIME_HOUR,timeHour);
intent.putExtra(TIME_MINUTE,timeMinute);
intent.putExtra(TONE,tone);
intent.putExtra(NOTE,note);
intent.putExtra(USER,userName);
return PendingIntent.getService(context,1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
@SuppressLint("NewApi")
private static void setAlarm(Context context, long nextUpdateTimeMillis, PendingIntent pIntent) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, nextUpdateTimeMillis, pIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, nextUpdateTimeMillis, pIntent);
}
}
}
并且工作正常但是当手机主屏幕上出现警报屏幕并且用户刚按下默认电话后退按钮时,它会从主屏幕上消失,声音和振动会继续,直到我停止运行任务。
答案 0 :(得分:1)
你所要做的就是覆盖onBackPressed()。
Android studio会在您输入onBackPressed()时自动填充此代码。
#
在super.onBackPressed()之前,你可能会做一些像贪睡或其他事情的事情。
另外你也可以使用onPause()或类似的那些。 (参考活动生命周期)
希望我清除你的怀疑。如果你有进一步的疑虑,请告诉我。答案 1 :(得分:0)
我认为"新波士顿"有一个很棒的视频来帮助理解这里发生的事情。 https://www.youtube.com/watch?v=-G91Hp3t6sg 你可能正在寻找onPause()来停止声音。 我认为该视频将是监控您的应用在不同状态下的表现的良好起点。