2016-12-14 37 views
0

所以,這裏是問題所在,我試圖做一個覆蓋顯示來自主應用程序的數據,應用程序運行時沒有錯誤,但實際上並未顯示數據。Android Studio - 顯示覆蓋活動中的主要活動的信息

Main.java:這是主要的應用程序,所有的數據輸入和其他的東西發生。

package com.schnetzapps.myapplication; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.CompoundButton; 
import android.widget.EditText; 
import android.widget.Switch; 
import android.widget.TextView; 

public class Main extends Activity { 
    Switch toggleOverlaySwitch; 
    TextView overlayLabel; 
    EditText testText; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setTheme(R.style.AppTheme); 
     setContentView(R.layout.activity_main); 

     LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View vi = inflater.inflate(R.layout.overlay, null); 
     overlayLabel = (TextView) vi.findViewById(R.id.overlayLabel); 
     toggleOverlaySwitch = (Switch) findViewById(R.id.openOverlay); 

     toggleOverlaySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       toggleService(); 
       testText = (EditText) findViewById(R.id.testText); 
       overlayLabel.setText(testText.getText()); 
       testText.setText(overlayLabel.getText()); 
      } 
     }); 
    } 

    private void toggleService(){ 
     Intent intent = new Intent(this, OverlayService.class); 
     if(!stopService(intent)){ 
      startService(intent); 
     } 
    } 
} 

Overlay.java:這是專爲用戶顯示信息,因爲它改變了不管什麼其他的應用程序可以是開放的疊加。 package com.schnetzapps.myapplication;

 import android.app.Service; 
     import android.content.Intent; 
     import android.graphics.Color; 
     import android.graphics.PixelFormat; 
     import android.os.IBinder; 
     import android.view.View; 
     import android.view.WindowManager; 
     import android.widget.LinearLayout; 

public class Overlay extends Service { 

    LinearLayout oView; 

    @Override 
    public IBinder onBind(Intent i) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     oView = new LinearLayout(this); 
     int col = Color.parseColor("#4286f4"); 
     oView.setBackgroundColor(col); 
     int width = 500; 
     int height = 500; 
     int x = 0; 
     int y = 0; 
     WindowManager.LayoutParams params = new WindowManager.LayoutParams(width, height, x, y, 
       WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, 
       WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 
         WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | 
         WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, 
       PixelFormat.TRANSLUCENT); 
     WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); 
     LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.overlay, null); 
     oView.addView(layout); 
     wm.addView(oView, params); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if(oView!=null){ 
      WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); 
      wm.removeView(oView); 
     } 
    } 

} 

activity_main.xml:這是主應用程序的設計。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <Switch 
     android:text="Open Overlay" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/openOverlay" /> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:text="Test" 
     android:ems="10" 
     android:id="@+id/testText" /> 
</LinearLayout> 

overlay.xml:這是覆蓋設計,我想設置的文本標籤。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/overlayLayout" > 
     <TextView 
      android:text="TextView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/overlayLabel" /> 
    </RelativeLayout> 
</LinearLayout> 

我沒有得到任何空引用異常,指出我指向錯誤的TextView,並且覆蓋作爲需要很好的切換,爲什麼是覆蓋不改變什麼已經進入文本EditText字段?

截圖 疊加不反轉要上,所以覆蓋不可見: enter image description here

的覆蓋已打開,但TextView中沒有顯示從EditText上的文字: enter image description here

我相信我失去了一些愚蠢的東西...任何輸入是非常感謝。

回答

0

經過一番擺弄周圍,我發現,解決方法當然是的意向系統:intent.putExtra(key, value);intent.getStringExtra(key);

在我上面的代碼我改變了下面的代碼:

private void toggleService(){ 
    Intent intent = new Intent(this, OverlayService.class); 
    if(!stopService(intent)){ 
     startService(intent); 
    } 
} 

要包括意向推杆:

private void toggleService(String key, String value){ 
    Intent intent = new Intent(this, OverlayService.class); 
    intent.putExtra(key, value); 
    if(!stopService(intent)){ 
     startService(intent); 
    } 
} 

而且在覆蓋服務,我改變大部分的代碼,因此它現在看起來像這樣:

package com.schnetzapps.myapplication; 

     import android.app.Service; 
     import android.content.Intent; 
     import android.graphics.Color; 
     import android.graphics.PixelFormat; 
     import android.os.IBinder; 
     import android.view.View; 
     import android.view.WindowManager; 
     import android.widget.LinearLayout; 
     import android.widget.TextView; 

public class OverlayService extends Service { 
    String data; 
    LinearLayout oView; 

    @Override 
    public IBinder onBind(Intent i) { 
     return null; 
    } 

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

    public int onStartCommand (Intent intent, int flags, int startId) { 
     data = intent.getStringExtra("testText"); 
     RunOverlay(); 
     return START_STICKY; 
    } 

    void RunOverlay() { 
     oView = new LinearLayout(this); 
     int col = Color.parseColor("#4286f4"); 
     oView.setBackgroundColor(col); 
     int width = 500; 
     int height = 500; 
     int x = 0; 
     int y = 0; 
     WindowManager.LayoutParams params = new WindowManager.LayoutParams(width, height, x, y, 
       WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, 
       WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 
         WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | 
         WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, 
       PixelFormat.TRANSLUCENT); 
     WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); 
     LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.overlay, null); 

     oView.addView(layout); 
     TextView t = (TextView)oView.findViewById(R.id.overlayLabel); 
     t.setText(data); 
     wm.addView(oView, params); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if(oView!=null){ 
      WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); 
      wm.removeView(oView); 
     } 
    } 
} 

結果當然是當切換改變時,覆蓋窗口將顯示來自EditText字段的文本。

enter image description here