I'm trying to add a window in my android app but I'm facing with this issue.
I just started android development so I'm not sure what should I need to do. I did research but couldn't find the solution yet. This is Window.java file.
package serhatkaya.hp;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
public class Window {
private Context context;
private WindowManager windowManager;
private LayoutInflater layoutInflater;
private View rootView;
private WindowManager.LayoutParams windowParams;
public Window(Context context) {
this.context = context;
this.windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.rootView = layoutInflater.inflate(R.layout.window, null);
this.windowParams = new WindowManager.LayoutParams(
0,
0,
0,
0,
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
: WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
initWindowParams();
initWindow();
}
private DisplayMetrics getCurrentDisplayMetrics() {
DisplayMetrics dm = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(dm);
return dm;
}
private void calculateSizeAndPosition(WindowManager.LayoutParams params, int widthInDp, int heightInDp) {
DisplayMetrics dm = getCurrentDisplayMetrics();
params.gravity = Gravity.TOP | Gravity.LEFT;
params.width = (int) (widthInDp * dm.density);
params.height = (int) (heightInDp * dm.density);
params.x = (dm.widthPixels - params.width) / 2;
params.y = (dm.heightPixels - params.height) / 2;
}
private void initWindowParams() {
calculateSizeAndPosition(windowParams, 300, 80);
}
private void initWindow() {
rootView.findViewById(R.id.window_close).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
close();
}
});
rootView.findViewById(R.id.content_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Adding notes to be implemented.", Toast.LENGTH_SHORT).show();
}
});
}
public void open() {
try {
windowManager.addView(rootView, windowParams);
} catch (Exception e) {
String d = e.getMessage();
// Ignore exception for now, but in production, you should have some
// warning for the user here.
}
}
public void close() {
try {
windowManager.removeView(rootView);
} catch (Exception e) {
// Ignore exception for now, but in production, you should have some
// warning for the user here.
}
}
}
AndroidManifest.xml
<?xml version='1.0' encoding='utf-8'?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-feature
android:name="android.hardware.telephony"
android:required="false" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:theme="@style/AppTheme" android:usesCleartextTraffic="true">
<activity
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
android:exported="true" android:label="@string/title_activity_main"
android:launchMode="singleTask" android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBarLaunch">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:authorities="${applicationId}.fileprovider" android:exported="false"
android:grantUriPermissions="true" android:name="androidx.core.content.FileProvider">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
When I try to open Window with open() function.
I'm facing the issue mentioned in the title.
I tried to add permission via androidmanifest.xml and also requesting from mainactivity like this:
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] PERMISSIONS = {
android.Manifest.permission.READ_PHONE_STATE,
android.Manifest.permission.READ_CALL_LOG,
android.Manifest.permission.PROCESS_OUTGOING_CALLS,
android.Manifest.permission.FOREGROUND_SERVICE,
android.Manifest.permission.SYSTEM_ALERT_WINDOW
};
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS, PackageManager.PERMISSION_GRANTED);
Window window = new Window(this);
window.open();
}
I faced the same issue.
if (!Settings.canDrawOverlays(this)) {
// ask for permission
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 1);
}
I found requesting permission for opening alert windows are changed after android 6.0
I requested permission like this and problem solved.
Note: startActivityForResult(intent,1) usage is deprecated so you may want to change it.