【问题标题】:Android - Activity is not closed by Back buttonAndroid - 活动没有被后退按钮关闭
【发布时间】:2017-02-13 01:03:50
【问题描述】:

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

服务:

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:已尝试完成输入事件,但输入事件接收器已被释放。”当我点击后退按钮时打印。这是造成这个问题的原因吗?

【问题讨论】:

    标签: android android-activity service back-button


    【解决方案1】:

    Back 事件被弹窗消费,你可以尝试完成弹窗的 onDismiss 活动,或者尝试使用 dispatch Key 事件。

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

    【讨论】:

      【解决方案2】:

      有些人会试图说服你这不是一个好的解决方案(出于某些原因),但我认为你可以通过在 finish( ):

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

      最好的。

      编辑:

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

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

      然后,在我的关闭方法中,我终止了进程。在这里工作。

      【讨论】:

      • 我认为我们启动服务的方式不同,这里是我,那里是你。在我的答案中进行了一些编辑,也许可以提供帮助。谢谢你,祝你好运。
      【解决方案3】:

      弹窗捕捉回压事件,你应该在弹窗的 KeyEvent 监听器中调用finish

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多