整个应用程序的浮动视图 [英] Floating view over the whole application
问题描述
我需要对我的所有应用程序有一个浮动视图.
我可以使用 Window_service,但我不需要我的视图位于应用程序之外.
仅在我的应用程序内.
I need to have a floating view over all of my application.
I can use Window_service but I don't need my view to be outside of the app.
Only inside my app.
我也不希望用户看到在其他应用上绘图"权限.
我还能怎么做?
I also don't want the user to see "draw on other app" permission.
How else can I do it?
推荐答案
我不得不做类似的事情.就我而言,我希望所有日志语句都显示在整个应用程序上方(类似于 Android 设备中的 Developer Options -> Show CPU Usage
设置).
I've had to do something similar to this. In my case, I wanted all of my logging statements to be shown above the whole application (Similar to the Developer Options -> Show CPU Usage
settings in an Android device).
就我而言,我只想在应用程序顶部显示一个 TextView
,但您几乎可以用自己的布局替换它.
In my case, I just wanted to display a TextView
on top of the app, but you can pretty much replace it with your own layout.
这是我使用的代码示例:
Here's an example of the code I used:
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);
}
}
}
然后你就可以从你的活动中连接到这个服务并设置它的文本,像这样:
Then you can just connect with this Service from your Activity and set it's text, like so:
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");
如果您不希望它位于其他应用之上,您当然需要在用户离开应用后立即销毁该服务.
You would of course need to destroy that service as soon as the user leaves the app if you don't want it to be on top of other apps.
这篇关于整个应用程序的浮动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!