2014-03-01 27 views
0

我已经按照以下教程介绍了如何使用窗口管理器在所有其他窗口之上添加简单视图(在教程中是其图像)。如何使用窗口管理器将LinearLayout作为system_alert添加

http://www.piwai.info/chatheads-basics/

它的工作原理是,当我试图与ImageView的使用它的魅力。当我尝试使用ViewGroup子类(如线性布局)进行操作时,没有显示任何内容,我不知道为什么。

这里是我的代码

package com.example.servicedialogexample; 

import android.app.ActionBar; 
import android.app.AlertDialog; 
import android.app.IntentService; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.PixelFormat; 
import android.util.Log; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.WindowManager; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class HelloService extends IntentService { 

    private WindowManager windowManager; 
    private LinearLayout m_viwAlert; 
    private ImageView im; 

    public HelloService() 
    { 
     super("HelloService"); 

     m_viwAlert = null; 
     windowManager = null; 
    } 

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

     windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); 

     m_viwAlert = new LinearLayout(this); 
     im = new ImageView(this); 
     im.setImageResource(R.drawable.ic_launcher); 


     WindowManager.LayoutParams params1 = new WindowManager.LayoutParams(
       WindowManager.LayoutParams.WRAP_CONTENT, 
       WindowManager.LayoutParams.WRAP_CONTENT, 
       WindowManager.LayoutParams.TYPE_PHONE, 
       WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 
       PixelFormat.TRANSLUCENT); 

     params1.gravity = Gravity.TOP | Gravity.LEFT; 

     ((LinearLayout)m_viwAlert).addView(im, params1); 

     WindowManager.LayoutParams params = new WindowManager.LayoutParams(
       WindowManager.LayoutParams.WRAP_CONTENT, 
       WindowManager.LayoutParams.WRAP_CONTENT, 
       WindowManager.LayoutParams.TYPE_PHONE, 
       WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 
       PixelFormat.TRANSLUCENT); 
     params.gravity = Gravity.TOP | Gravity.LEFT; 
     params.x = 0; 
     params.y = 100; 

     windowManager.addView(m_viwAlert, params); 

    } 

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

     if (m_viwAlert != null) 
     { 
      windowManager.removeView(m_viwAlert); 
     } 

    } 
} 

我不知道我在做什么错误在这里...提前

感谢这里帮助我:)

回答

0

建议;抱歉。

我把你的代码复制过来并试着让我自己看看我能不能帮忙。我在onCreate和onDestroy中放了一些日志,结果发现onDestroy被立即调用。

现在我不知道很多关于IntentServices自己,所以,但我用

startService(context, HelloService.class); 

也许你应该检查你的代码不这样做,因为事情是开始它;我将它从IntentService更改为Service;以及你的代码工作的一种享受。

快乐调查;我希望能有更多的帮助。

相关问题