gpt4 book ai didi

android - 服务与 WindowManager 和通知

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:16:38 26 4
gpt4 key购买 nike

我有一个服务。我已经使用 WindowManager 在服务运行期间显示一些 UI。此外,我在此服务的生命周期内显示通知。

下面是我的服务类

public class DemoService extends Service implements View.OnClickListener {

private static final int mLayoutParamFlags = WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;

// Views
private View mDemoView;
private ImageButton mEndButton

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

showNotification();

return START_STICKY;
}

@Override
public void onCreate() {
super.onCreate();

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
mDemoView = layoutInflater.inflate(R.layout.windowmanager_demo, null);

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
mLayoutParamFlags,
PixelFormat.TRANSLUCENT);

mEndButton = (Button) view.findViewById(R.id.end_button);
mEndButton.setOnClickListener(this);

WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
windowManager.addView(mDemoView, params);
}

@Override
public void onDestroy() {
super.onDestroy();
// Remove notification
stopForeground(true);
// Remove WindowManager
if(mDemoView != null){
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.removeView(mDemoView);
}
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.end_button:
endService();
break;
}
}

/**
* Show notification
*/
private void showNotification() {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.demoicon)
.setContentTitle("Demo App")
.setContentText("Demo Message");

startForeground(R.string.demo_string, notificationBuilder.build());
}

private void endService() {
// Stop demo service
stopSelf();
}

以下是我想实现但不知道如何实现的东西

  1. 当服务正在运行且 WindowManager 的用户界面可见时,如果我按下硬件返回/主页键,那么 WindowManager 的用户界面应该会消失,而服务应该会继续运行。我不知道如何在服务中捕捉回压力。

  2. 当我点击通知时,WindowManager 的 UI 应该可见。

请帮我实现这两件事。

最佳答案

  1. 要捕获返回键,请扩展您要添加到 WindowManager 的 ViewGroupView,并覆盖 onKeyUp , 或 dispatchKeyEvent (并检查 keyCode 并跟踪 ACTION_UP)。请注意,为确保它有效,您必须检查您的 LayoutParams标记并且它的类型使您的 View 可聚焦(例如,使用 TYPE_SYSTEM_OVERLAY ,窗口无法聚焦),但是当您的 View 折叠时(如果您执行类似 Facebook Messenger Chat Heads 的操作),您必须更新您的 View LayoutParams 以放置标志 FLAG_NOT_FOCUSABLE .如果您不这样做,按下后退键将不会被分派(dispatch)到底层 Activity,而只会被分派(dispatch)到您的窗口,这会使用户卸载您的应用。
  2. 当您向通知添加操作时,您使用 PendingIntent .在 pendingIntent 中,您可以添加显式 Intent。它可以是服务操作(不用担心,您的服务不能启动两次,只有 onStartCommand 将在运行实例上调用,如果已经启动的话)、BroadcastReceiver 或 Activity。服务操作可能是您想要的,但如果合适,您也可以使用 BroadcastReceivers。

请注意,Marshmallow 要求您将用户带到“设置”屏幕,让他手动为您的应用启用“绘制其他应用”权限。您可以在 Android Developers page on G+ 上找到更多信息,他们最近发布了有关运行时权限的信息。

关于android - 服务与 WindowManager 和通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33742486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com