Android - 识别service.class中的HOME按钮

问题描述 投票:0回答:1

我正在开发一个永久的主屏幕覆盖对话框,因为按下主页按钮后服务启动活动时会有5秒的延迟我也被迫将我的活动代码放入服务中。我的应用程序工作正常,但当用户按下主页或后退按钮时,覆盖仍然在屏幕上。是否有任何方法来检测用户是否按下了主页或返回服务.class,我可以调用selfStop()并从屏幕中删除视图?在一个活动中,我可以使用onStop来检查是否按下了主页按钮,但由于服务中没有onStop(),我被卡住了。

任何方式让主页按钮按预期反应?

android android-service android-homebutton
1个回答
0
投票

移动后门和主页按钮在服务类中运行良好。

public class MyFooterService extends Service{
View myview;
WindowManager wm;
FrameLayout wrapper;
public MyFooterService() {

}
@Override
public void onCreate() {
Log.d(TAG, "onCreate called");
LayoutInflater li = (LayoutInflater) 
getSystemService(LAYOUT_INFLATER_SERVICE);
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
final WindowManager.LayoutParams params = new 
WindowManager.LayoutParams(
        WindowManager.LayoutParams.MATCH_PARENT,
        90,  //height of ur layout
        WindowManager.LayoutParams.TYPE_PHONE,
        0,
        PixelFormat.TRANSLUCENT);

params.gravity = Gravity.BOTTOM ;
params.x = 0;
params.y = 0;
wrapper = new FrameLayout(this) {
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
          // handle the back button code;
         return true;
        }
      return super.dispatchKeyEvent(event);
    }
   //if pressed home key,
  public void onCloseSystemDialogs(String reason) {
        //The Code Want to Perform.
        System.out.println("System dialog " + reason);
        if (reason.equals("homekey")) {
            // handle home button 
        }
    }

 };
 myview = li.inflate(R.layout.my_footer, wrapper);   // here set into your own layout

 wm.addView(myview, params);

   }
}
© www.soinside.com 2019 - 2024. All rights reserved.