安卓 - 活动未通过“后退”按钮关闭



我编写了活动(由服务调用(来制作弹出窗口,并在单击屏幕上的按钮或按后退按钮时完成了活动和服务。这是代码。

服务:

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();
  }
}

活动:

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);
  }
}

当我按下屏幕上的按钮时,一切都如我预期的那样顺利。但是当我按下后退按钮时,它只会关闭弹出窗口,服务和活动没有完成。当我再次单击后退按钮时,服务和活动已完成,但我想通过一键完成所有这些操作。

我也尝试了这个而不是onKeydown((,

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent i = new Intent(AlarmPopup.this, AlarmService.class);
    stopService(i);
    popup.dismiss();
    finish();
}

但它也不起作用。在这种情况下,我需要做什么?

哦,还有一件事。当我单击后退按钮时,会打印日志"W/InputEventReceiver:尝试完成输入事件,但输入事件接收器已被释放"。这是导致此问题的原因吗?

Back 事件

被弹窗消耗,可以尝试在弹出窗口的关闭上完成活动,或者尝试使用调度键事件。

  popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
      @Override
      public void onDismiss() {
          Intent i = new Intent(AlarmPopup.this, AlarmService.class);
          stopService(i);
          finish();
      }
  });

有些人会试图说服你这不是一个好的解决方案(出于某些原因(,但我认为你可以按照自己的方式,通过在 finish(( 之后将其添加到您的代码中来强制您的服务停止:

android.os.Process.killProcess(android.os.Process.myPid());

最好。

编辑:

我在onCreate上开始我的服务,这样做:

intent = new Intent(this, ServiceActivity.class);
    startService(intent);

然后,在我的关闭方法上,我杀死了进程。在这里工作。

弹出窗口捕获反压事件,您应该在弹出窗口的 KeyEvent listerner 中调用 finish

最新更新