Android 整个应用程序的浮动视图
我需要在我的所有应用程序上有一个浮动视图。Android 整个应用程序的浮动视图,android,view,Android,View,我需要在我的所有应用程序上有一个浮动视图。 我可以使用视窗服务,但我不需要将我的视图置于应用程序之外。 仅在我的应用程序中 我也不希望用户看到“使用其他应用程序”权限。 我还能怎么做呢?我不得不做类似的事情。在我的例子中,我希望所有的日志语句都显示在整个应用程序的上方(类似于Android设备中的Developer Options->Show CPU Usage设置) 在我的例子中,我只想在应用程序顶部显示一个TextView,但你几乎可以用自己的布局来替换它 下面是我使用的代码示例: impo
我可以使用视窗服务,但我不需要将我的视图置于应用程序之外。
仅在我的应用程序中 我也不希望用户看到“使用其他应用程序”权限。
我还能怎么做呢?我不得不做类似的事情。在我的例子中,我希望所有的日志语句都显示在整个应用程序的上方(类似于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);
}
}
}
然后,您可以通过活动连接此服务并设置其文本,如下所示:
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");
如果你不想让该应用程序位于其他应用程序之上,你当然需要在用户离开应用程序后立即销毁该服务。你可以在活动中构建应用程序,该活动包含你的浮动视图和片段,因此当你需要更改“背景”时内容你只需要修改片段…这不需要许可吗?啊哈,好的,是的。不记得是哪一个,但我想是系统警报窗口。我需要一个没有“使用其他应用”权限的。是的,我在回复ha后看到了。创建自定义Toast消息怎么样?您可以将自己的自定义布局添加到Toast中,而且,您几乎可以在任意时间内保持Toast的打开状态。这是我几年前开发的一个简单技巧:你把我带到了那里。我不认为祝酒会支持点击事件。也许你可以看看真正的Toast源代码,看看你是否至少能在其他视图之上找到视图部分:祝你好运!