我需要启动一个弹出窗口,从服务做具体的我的应用程序中的东西。我似乎还没有找到一个很好的办法做到这一点。里面的onCreate()创建的弹出窗口,但是当我启动它showAtLocation我得到这个错误:
10-06 13:39:49.573:ERROR / AndroidRuntime(5966):android.view.WindowManager $ BadTokenException:产生的原因无法添加窗口 - 标记空是无效的;在您的活动运行?
有人可以帮忙吗?谢谢你,
-em
解决方案我有同样的问题,在这里,如果我是如何解决它。
在我第一次尝试使用Android的原生 PopupWindow
类,但后来我决定使用窗口管理器
这样的方式
公共类的MyService延伸服务{
私人查看MView的;
私人WindowManager.LayoutParams mParams;
私人窗口管理mWindowManager;
@覆盖
公共无效的onCreate(){
super.onCreate();
MVIEW =新MyLoadView(本);
mParams =新WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,150,10,10,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
mParams.gravity = Gravity.CENTER;
mParams.setTitle(窗口测试);
mWindowManager =(窗口管理器)getSystemService(WINDOW_SERVICE);
mWindowManager.addView(MVIEW,mParams);
}
@覆盖
公众的IBinder onBind(意向意图){
返回null;
}
@覆盖
公共无效的onDestroy(){
super.onDestroy();
((窗口管理器)getSystemService(WINDOW_SERVICE))removeView(MVIEW)。
MVIEW = NULL;
}
公共类MyLoadView扩展视图{
私人油漆mPaint;
公共MyLoadView(上下文的背景下){
超(上下文);
mPaint =新的油漆();
mPaint.setTextSize(50);
mPaint.setARGB(200,200,200,200);
}
@覆盖
保护无效的OnDraw(帆布油画){
super.onDraw(画布);
canvas.drawText(测试测试测试,0,100,mPaint);
}
@覆盖
保护无效onAttachedToWindow(){
super.onAttachedToWindow();
}
@覆盖
保护无效onDetachedFromWindow(){
super.onDetachedFromWindow();
}
@覆盖
保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
}
}
}
和您可以通过意向调用这个服务类:
意图=新的意图(mContext,MyService.class);
如果(意向!= NULL){
。的getContext()startService(意向);
}
此外,在您的清单文件设置权限:
<使用-权限的Android:名称=android.permission.SYSTEM_ALERT_WINDOW/>
我引用Android的 LoadAverageService.java
类,我终于从服务创建的弹出窗口!这是一个非常简单的例子,所以你可能需要添加更多的东西,如果你想开发复杂的功能。希望这会帮助你和其他人。
I need to launch a popup window from a service to do something specific inside my application. I do not seem to find a good way to do it. Inside onCreate() I create the popup window but when I launch it with showAtLocation I get this error:
10-06 13:39:49.573: ERROR/AndroidRuntime(5966): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
Can someone please help? Thanks,
-em
解决方案I had the same problem and here if how i solved it.
At first time i've tried to use Android's native PopupWindow
class, but later i decided to use WindowManager
like this way :
public class MyService extends Service {
private View mView;
private WindowManager.LayoutParams mParams;
private WindowManager mWindowManager;
@Override
public void onCreate() {
super.onCreate();
mView = new MyLoadView(this);
mParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT, 150, 10, 10,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
mParams.gravity = Gravity.CENTER;
mParams.setTitle("Window test");
mWindowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mView, mParams);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
((WindowManager)getSystemService(WINDOW_SERVICE)).removeView(mView);
mView = null;
}
public class MyLoadView extends View {
private Paint mPaint;
public MyLoadView(Context context) {
super(context);
mPaint = new Paint();
mPaint.setTextSize(50);
mPaint.setARGB(200, 200, 200, 200);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText("test test test", 0, 100, mPaint);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
And you can call this service class via intent :
intent = new Intent(mContext, MyService.class);
if (intent != null) {
getContext().startService(intent);
}
Also, set permission in your manifest file :
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
I referenced Android's LoadAverageService.java
class, and i finally created a popup window from service! This is a very simple example, so you might need to add something more if you want to develop complex functions. Hope this will help you and others.