2016-12-14 2 views
0

ここに問題がありますが、メインアプリからのデータを表示するオーバーレイを作成しようとしていますが、アプリはエラーなしで実行されていますが、 。Androidスタジオ - オーバーレイアクティビティの主なアクティビティから情報を表示

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:これは、それは関係なく開くことがあり、他のどのようなアプリを変更しないなどのユーザ情報を表示するように設計されたオーバーレイです。 パッケージ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:これは、テキストを設定しようとしているラベルのオーバーレイデザインです。

私は私が間違っているのTextViewを指しています、そして必要に応じてオーバーレイがうまくトグルしている、なぜオーバーレイ上のテキストを締結したものに変更されていないことを示す任意のnull参照の例外を取得していないです
<?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> 

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

関連する問題