美文网首页高级UI
dialog之permission denied for win

dialog之permission denied for win

作者: Ad大成 | 来源:发表于2020-07-24 15:26 被阅读0次

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

}

以上就是关于系统级别弹窗的问题

相关文章

网友评论

    本文标题:dialog之permission denied for win

    本文链接:https://www.haomeiwen.com/subject/bhdclktx.html