Android - Activity 未通过后退按钮关闭
Android - Activity is not closed by Back button
我编写了 activity(由服务调用)来制作弹出窗口window,并在我单击屏幕上的按钮或按下后退按钮时完成了 activity 和服务。这是代码。
服务:
public class AlarmService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent popupIntent = new Intent(AlarmService.this, AlarmPopup.class);
popupIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(popupIntent);
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
Activity:
public class AlarmPopup extends AppCompatActivity {
PopupWindow popup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
onShowPopup();
}
};
handler.postDelayed(r, 500);
}
public void onShowPopup() {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.alarm_popup, null);
popup = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, true);
popup.setBackgroundDrawable(new BitmapDrawable());
popup.showAtLocation(view, Gravity.CENTER, 0, 0);
view.findViewById(R.id.button).setOnClickListener(mClickListener);
}
Button.OnClickListener mClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(AlarmPopup.this, AlarmService.class);
stopService(i);
popup.dismiss();
finish();
}
};
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent i = new Intent(AlarmPopup.this, AlarmService.class);
stopService(i);
popup.dismiss();
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
当我按下屏幕上的按钮时,一切都如我所料顺利进行。但是当我按下后退按钮时,它只关闭弹出窗口 window,并且服务和 activity 没有完成。服务和Activity在我再次点击后退按钮时完成,但我想一键完成所有这些。
我也试过这个而不是 onKeydown(),
@Override
public void onBackPressed() {
super.onBackPressed();
Intent i = new Intent(AlarmPopup.this, AlarmService.class);
stopService(i);
popup.dismiss();
finish();
}
但它也不起作用。这种情况我需要做什么?
哦,还有一件事。单击后退按钮时打印日志 'W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.'。这是这个问题的原因吗?
有些人会试图说服您这不是一个好的解决方案(出于某些原因),但我认为您可以按照自己的方式强制停止服务,方法是在 [=24 之后将其添加到您的代码中=]完成():
android.os.Process.killProcess(android.os.Process.myPid());
最佳。
编辑:
我在 onCreate 上开始我的服务,方法是:
intent = new Intent(this, ServiceActivity.class);
startService(intent);
然后,在我的关闭方法中,我终止了进程。在这里工作。
Popup window 捕获 backpress 事件,你应该在 popup window 的 KeyEvent listerner
中调用 finish
Back事件被popup消耗window,你可以尝试完成activitypopup的onDismiss,或者尝试使用dispatch Key事件。
popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
Intent i = new Intent(AlarmPopup.this, AlarmService.class);
stopService(i);
finish();
}
});
我编写了 activity(由服务调用)来制作弹出窗口window,并在我单击屏幕上的按钮或按下后退按钮时完成了 activity 和服务。这是代码。
服务:
public class AlarmService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent popupIntent = new Intent(AlarmService.this, AlarmPopup.class);
popupIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(popupIntent);
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
Activity:
public class AlarmPopup extends AppCompatActivity {
PopupWindow popup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
onShowPopup();
}
};
handler.postDelayed(r, 500);
}
public void onShowPopup() {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.alarm_popup, null);
popup = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, true);
popup.setBackgroundDrawable(new BitmapDrawable());
popup.showAtLocation(view, Gravity.CENTER, 0, 0);
view.findViewById(R.id.button).setOnClickListener(mClickListener);
}
Button.OnClickListener mClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(AlarmPopup.this, AlarmService.class);
stopService(i);
popup.dismiss();
finish();
}
};
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent i = new Intent(AlarmPopup.this, AlarmService.class);
stopService(i);
popup.dismiss();
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
当我按下屏幕上的按钮时,一切都如我所料顺利进行。但是当我按下后退按钮时,它只关闭弹出窗口 window,并且服务和 activity 没有完成。服务和Activity在我再次点击后退按钮时完成,但我想一键完成所有这些。
我也试过这个而不是 onKeydown(),
@Override
public void onBackPressed() {
super.onBackPressed();
Intent i = new Intent(AlarmPopup.this, AlarmService.class);
stopService(i);
popup.dismiss();
finish();
}
但它也不起作用。这种情况我需要做什么?
哦,还有一件事。单击后退按钮时打印日志 'W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.'。这是这个问题的原因吗?
有些人会试图说服您这不是一个好的解决方案(出于某些原因),但我认为您可以按照自己的方式强制停止服务,方法是在 [=24 之后将其添加到您的代码中=]完成():
android.os.Process.killProcess(android.os.Process.myPid());
最佳。
编辑:
我在 onCreate 上开始我的服务,方法是:
intent = new Intent(this, ServiceActivity.class);
startService(intent);
然后,在我的关闭方法中,我终止了进程。在这里工作。
Popup window 捕获 backpress 事件,你应该在 popup window 的 KeyEvent listerner
中调用finish
Back事件被popup消耗window,你可以尝试完成activitypopup的onDismiss,或者尝试使用dispatch Key事件。
popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
Intent i = new Intent(AlarmPopup.this, AlarmService.class);
stopService(i);
finish();
}
});