浮动整个应用程序的视图

时间:2014-01-30 19:21:31

标签: android view

我需要对我的所有应用程序都有一个浮动视图 我可以使用Window_service,但我不需要我的视图在应用程序之外。
只在我的应用内。

我也不希望用户看到“在其他应用上绘制”权限 我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

我必须做类似的事情。就我而言,我希望我的所有日​​志记录语句都显示在整个应用程序的上方(类似于Android设备中的Developer Options -> Show CPU Usage设置)。

在我的情况下,我只想在应用程序的顶部显示TextView,但您几乎可以用自己的布局替换它。

以下是我使用的代码示例:

import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Binder;
import android.os.IBinder;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.BackgroundColorSpan;
import android.view.WindowManager;
import android.widget.TextView;

public class OverlayService extends Service {

    private final IBinder mBinder = new LocalBinder();
    private TextView mTextView;
    private Spannable mSpannable;

    public class LocalBinder extends Binder {
        public OverlayService getService() {
            return OverlayService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

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

        mTextView = new TextView(this);
        mTextView.setTextSize(10);
        mTextView.setTextColor(Color.WHITE);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 0 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.addView(mTextView, params);
    }

    public void setText(String text) {

        mSpannable = new SpannableString(text);
        mSpannable.setSpan(new BackgroundColorSpan(0xD993d2b9),0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);           

        // simple logic to only display 10 lines
        if(mTextView.getLineCount() > 10) {
            mTextView.setText(mSpannable);
        } else {
            mTextView.append(mSpannable);
        }
        mTextView.append("\n");

    }

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

然后你可以从你的Activity连接这个服务并设置它的文本,如下所示:

ServiceConnection mConnection = new ServiceConnection() {

                        @Override
                        public void onServiceConnected(ComponentName className, IBinder service) {
                                LocalBinder binder = (LocalBinder) service;
                                mService = binder.getService();
                        }

                        @Override
                        public void onServiceDisconnected(ComponentName arg0) {
                        }
                };

                Intent intent = new Intent(context, OverlayService.class);
                context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
                mService.setText("Sending a test message");

当用户离开应用程序时,如果您不希望它在其他应用程序之上,您当然需要销毁该服务。