2014-03-01 25 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;以及你的代碼工作的一種享受。

快樂調查;我希望能有更多的幫助。

相關問題