1

I want add floating icon but i received error as title. I added permisson android:name="android.permission.SYSTEM_ALERT_WINDOW in manifest. I hope to get help from comunication.

Error

/AndroidRuntime: FATAL EXCEPTION: main Process: com.dxlampro.floatingapp, PID: 7639 android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@f07c325 -- permission denied for window type 2038 at android.view.ViewRootImpl.setView(ViewRootImpl.java:789) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93) ....

Bound Service

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); } } 

MainActivity

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
  • 1
    From official docs(SYSTEM_ALERT_WINDOW): If the app targets API level 23 or higher, the app user must explicitly grant this permission.... ... then there is how to ask and how to check ... I do not see this code here Commented Oct 24, 2022 at 13:46

1 Answer 1

2

Add this in AndoridManifest.xml file

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 

and ask to grant the permission:

Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); intent.setData(Uri.parse("package:" + getPackageName())); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.