dialog之permission denied for window type 2003

https://blog.csdn.net/f552126367/article/details/80497583
關於系統或者說全局dialog 注意的就是兩點 第一安卓6.0以下默認權限開啓 以上需要申請可以懸浮在其他app以上的權限
第二就是這個 dialog.getWindow().setType((WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY));
TYPE_SYSTEM_ALERT 這個已經廢棄了 所以出現type2003的主要原因就是這個 再有就是清單文件裏註冊權限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

下面貼以下代碼


    private void setPermission() {
        if(Build.VERSION.SDK_INT>=23)
        {
            if(Settings.canDrawOverlays(this))
            {
                //有懸浮窗權限開啓服務綁定 綁定權限
                Intent intent = new Intent(MainActivity.this, PushService.class);
                startService(intent);

            }else{
                //沒有懸浮窗權限m,去開啓懸浮窗權限
                try{
                    Intent  intent=new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                    startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
                }catch (Exception e)
                {
                    e.printStackTrace();
                }

            }
        } else{
            //默認有懸浮窗權限  但是 華爲, 小米,oppo等手機會有自己的一套Android6.0以下  會有自己的一套懸浮窗權限管理 也需要做適配
            Intent intent = new Intent(MainActivity.this, PushService.class);
            startService(intent);
        }
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
            if(Build.VERSION.SDK_INT>=23) {
                if (!Settings.canDrawOverlays(this)) {
                    Toast.makeText(this, "權限授予失敗,無法開啓懸浮窗", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "權限授予成功!", Toast.LENGTH_SHORT).show();
                    //有懸浮窗權限開啓服務綁定 綁定權限
                    Intent intent = new Intent(MainActivity.this, PushService.class);
                    startService(intent);
                }
            }
        }
    }

服務代碼

public class PushService extends Service {

    private Handler handler;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("PushService", "onCreate: ");
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, ShowNotificationReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.currentThreadTimeMillis(),pendingIntent);

        Intent intent1 = new Intent(this, WTWebViewNotificationReceiver.class);
        PendingIntent broadcast = PendingIntent.getBroadcast(this, 1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        am.set(AlarmManager.ELAPSED_REALTIME,SystemClock.currentThreadTimeMillis(),broadcast);

        handler = new Handler(Looper.getMainLooper());
        systemDialog();
    }

    private void systemDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("該下車了");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        final AlertDialog dialog = builder.create();
    //在dialog  show方法之前添加如下代碼,表示該dialog是一個系統的dialog**
        dialog.getWindow().setType((WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY));
        new Thread(){
            public void run() {
                SystemClock.sleep(4000);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        dialog.show();
                    }
                });
            };
        }.start();
    }

}

以上就是關於系統級別彈窗的問題

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章