我们从启动服务的活动开始,然后变为空闲状态,直到服务再次调用活动为止。该活动去onPause(我相信),并不会开始备份,直到其BroadcastReceiver获得其呼叫开始。我的服务创建一个WindowManager作为系统覆盖层,当用户触发它时,它应该调用该活动并使活动创建一个新的视图,其中包含一些内容。那么一切都会顺利进行,没有错误弹出窗口,我的所有日志都表明代码运行顺利,但应该做出的新视图永远不会被看到。有趣的是,如果你点击应用程序重新启动它,弹出我以前想要的视图。所以我很好奇,为什么它不会在我想要的时候发生,但是它仍然是不可见的。我已经尝试将视图设置为可见,尝试bringToFront(),我甚至尝试了一些标志意图将活动带到栈顶,但没有任何工作。相关代码如下。在制作WindowManager之后使用setContentView()问题
我不是在这里听说为什么你不认为我的应用程序对用户有好处,或者你不认为它遵循android设计“设计”我只是想帮助调试这个有趣的错误。
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent); //if i comment out these lines and add the call
initService(); //to figureOutParam below it works perfectly
sendUserHome(); //this one as well
figureOutParam("UPDATE",0); //this is the call that i use to get through to
//the method though right now neither param is important
}
public void figureOutParam(String param, int top){
if(param.equals("UPDATE")){
View myView = new View(this);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.activity_main, null);
myView.setBackgroundColor(Color.BLUE);
myView.bringToFront();
setContentView(myView);
}
}
为MyService(涉及回调到服务只重要的部分)
@Override
public void onCreate() {
super.onCreate();
WindowManager window = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
Display d = window.getDefaultDisplay();
int width=d.getWidth();
int height=d.getHeight();
height= (int)(height*.025);
params = new WindowManager.LayoutParams(WindowManager.LayoutParams.FILL_PARENT,height,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity=Gravity.BOTTOM;
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
}
public void updateView(String params){
Intent i = new Intent(MY_INTENT);
i.putExtra("y", this.y);
i.putExtra("param", params);
sendBroadcast(i);
}
清单,因为,因为
而这个调用将工作,我不想访问figureOutParam,直到它从服务调用。和它的问题是,当服务调用它时,一切顺利,但我在figureOutParam中的视图不可见(即使我将它设置为可见并将其设置为bringToFront())。所以是的,这将工作,但它不会帮助我的代码 –