我想添加浮动图标,但我收到了错误的标题。我添加了permisson android:name ="android. permission. SYSTEM_ALERT_WINDOW在清单中。我希望从通信获得帮助。
错误
/安卓运行时:致命异常:主要工艺:com. dxlampro.浮动应用程序,PID:7639安卓系统 windows 管理器出现错误$BadToken异常:无法添加窗口android.view.ViewRootImpl$W@f07c325--在android上对窗口类型2038的权限被拒绝。在android上对视图. ViewRootImpl.setView(ViewRootImpl.java:789)的权限被拒绝。在android上对视图.窗口管理器全局. addView(窗口管理器全局. java:356)的权限被拒绝。
绑定服务
package com.unity3d.player;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import com.dxlampro.floatingapp.R;
public class FloatingMenu extends Service {
private final IBinder binder = new LocalBinder();
private WindowManager.LayoutParams params;
private WindowManager mWindowManager;
public class LocalBinder extends Binder {
public FloatingMenu getService() {
return FloatingMenu.this;
}
}
@Override
public void onCreate() {
Toast.makeText(this.getBaseContext(),"On Create Service",Toast.LENGTH_SHORT).show();
super.onCreate();
}
@Override
public void onDestroy() {
Toast.makeText(this.getBaseContext(),"On Destroy Service",Toast.LENGTH_SHORT).show();
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return binder;
}
public void startFloating(){
View mFloatingView = LayoutInflater.from(this).inflate(R.layout.floating_view, null);
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
}
params.gravity = Gravity.CENTER; //Initially view will be added to top-left corner
params.x = 0;
params.y = 100;
//Add the view to the window
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mFloatingView, params);
}
}
主要活动
package com.dxlampro.floatingapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import com.unity3d.player.FloatingMenu;
public class MainActivity extends AppCompatActivity {
private FloatingMenu myService;
private boolean isBound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this,FloatingMenu.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection connection = new ServiceConnection() {
// Phương thức này được hệ thống gọi khi kết nối tới service bị lỗi
@Override
public void onServiceDisconnected(ComponentName name) {
isBound = false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
FloatingMenu.LocalBinder binder = (FloatingMenu.LocalBinder) service;
myService = binder.getService();
myService.startFloating();
isBound = true;
}
};
@Override
protected void onPause() {
super.onPause();
isBound = false;
}
}
1条答案
按热度按时间q5lcpyga1#
将其添加到AndoridManifest.xml文件中
并请求授予许可: