我创建了一个在屏幕上应用彩色滤镜的应用。我希望能够通过按下按钮来更改过滤器的颜色。更改xml文件中的背景颜色有效,但是当我尝试在服务中更改它时,它不会改变颜色。我没有错,但颜色没有变化。我认为,因为我使用windowmanager添加视图而不是使用setContentView
设置它,它可能不会应用颜色更改。这是我的服务代码,我知道正在调用该服务,因为Log.d(TAG,"onCreated");
出现在logcat中。顺便说一下,我尝试更改root
而不是bView
的颜色,但最终得到的结果相同。
public class ChangeColor extends Service {
public static final String TAG = "ChangeColour";
View mOverlayView;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mOverlayView = inflater.inflate(R.layout.overlay_view, null);
LinearLayout bView = (LinearLayout) mOverlayView.findViewById(R.id.lin);
if (bView != null) {
View root = bView.getRootView();
bView.setBackgroundColor(Color.parseColor("#000000"));
Log.d(TAG, "onCreated");
}
return START_NOT_STICKY;
}
}
如果您想查看添加视图的服务和活动,请参阅my old post。如果有人有任何想法让我知道!感谢。
答案 0 :(得分:0)
尝试以下方法。我没看到你把视图添加到窗口的位置,虽然我知道你以前在那里有过。我也改变了颜色和一些值,所以我注意到了变化。确保在清单中定义了服务。
public class ChangeColor extends Service {
public static final String TAG = "ChangeColour";
View mOverlayView;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
params.alpha = 0.5F;
params.dimAmount = 0.9F;
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mOverlayView = inflater.inflate(R.layout.overlay_view, null);
wm.addView(mOverlayView, params);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mOverlayView != null) {
mOverlayView.setBackgroundColor(Color.parseColor("#FF0000"));
}
return START_NOT_STICKY;
}
}