【发布时间】:2016-02-01 10:19:56
【问题描述】:
我有一个有趣的问题。我在服务中有一个 WindowManager。现在窗口管理器覆盖了屏幕并且是半透明的,所以你可以在窗口管理器下做其他事情。
在启动窗口管理器服务的应用程序中。我想通过按下按钮更改颜色来更改背景颜色。但是,我无法获得该视图,或者不知道如何使其能够使用它。 这是项目的github,希望有人能帮助我,如果您需要更多信息,请告诉我。
MainActivity中有一个onClick方法setDiffColor可以改变颜色可以使用R.color.colorAccent来改变颜色到那个
https://github.com/juangdiaz/windowmanagerservice
这是我在 MainActivity 中启动服务的方式
public void setOn(View v) {
Toast.makeText(this, "opening window", Toast.LENGTH_SHORT).show();
setOn = (Button) findViewById(R.id.button);
setOn.setEnabled(false);
sd.start(sensorManager);
startService(new Intent(this, FloatingWidgetService.class));
}
在 MainActivity 中停止服务
public void setOff(View v) {
setOn.setEnabled(true);
stopService(new Intent(getApplication(), FloatingWidgetService.class));
}
在 MainActivity 中更改颜色
public void setDiffColor(View v) {
R.color.colorAccent
//TODO: Change the WindowManagers Color
}
浮动小部件服务类
package com.bluetooth.juandiaz.bluetoothconnectivity.services;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import com.bluetooth.juandiaz.bluetoothconnectivity.R;
public class FloatingWidgetService extends Service {
private WindowManager windowManager;
private View widget;
WindowManager.LayoutParams params;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
widget = new View(this);
final LayoutInflater factory = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
widget = factory.inflate(R.layout.floating_widget, null);
widget.setAlpha(0.50f);
params= new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
windowManager.addView(widget, params);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
if (widget != null)
windowManager.removeView(widget);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
【问题讨论】:
-
请澄清。您正在谈论一项服务和一项活动,但不清楚您想要什么。也提供代码!
-
代码在 repo 中,我把它简单化了,所以很容易理解,在主要活动中我有一个服务,当我启动服务时,它会创建一个 WindowManager,它会一直停留在屏幕上因为你不停止服务。我希望能够通过按下 mainactivity 上的按钮来更改它的背景颜色
-
请在此处发布代码的相关部分,因为将来可能会删除第 3 方链接(更不用说,我们解决问题后,您将提交修复,所以没有人即使有新版本也会看到问题)
-
现在就知道了
标签: android android-service android-windowmanager