dialog必须依附于activity吗

android开发中悬浮窗被禁用,无权限开启悬浮窗的解决方案_xiao_ma123的专栏-CSDN博客首先,感谢这两篇博文http://blog.csdn.net/cankingapp/article/details/51569576 http://blog.csdn.net/cool_fuwei/article/details/53070232问题一:在安卓开发中处理悬浮窗权限时,发现魅族和小米手机无论android4.4.4、android5.1.1还是android6.0,权限manifeshttps://blog.csdn.net/siwujidan0125/article/details/53283876

是如果说有些情况下获取不到Activity对象,但是又想弹出Dialog呢?这时候用全局的Dialog是可以实现的。

只需要设置dialog为WindowManager.LayoutParams.TYPE_SYSTEM_ALERT类型

然后添加android.permission.SYSTEM_ALERT_WINDOW权限

1
2
3
4
5
6
7
8
9
10
Dialog dialog = new AlertDialog.Builder(activity.getApplicationContext())
        .setTitle("野猿新一")
        .setMessage("我是对话框内容啦")
        .create();
// 增加这句代码
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();

// 然后在manifest中添加权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

/**
 * 判断悬浮窗权限
 *
 * @param context
 * @return
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
public static boolean isFloatWindowOpAllowed(Context context) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 19) {
        return checkOp(context, 24);  // AppOpsManager.OP_SYSTEM_ALERT_WINDOW
    } else {
        if ((context.getApplicationInfo().flags & 1 << 27) == 1 << 27) {
            return true;
        } else {
            return false;
        }
    }
}

@TargetApi(Build.VERSION_CODES.KITKAT)
public static boolean checkOp(Context context, int op) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 19) {
        AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        try {
            Class<?> spClazz = Class.forName(manager.getClass().getName());
            Method method = manager.getClass().getDeclaredMethod("checkOp", int.class, int.class, String.class);
            int property = (Integer) method.invoke(manager, op,
                    Binder.getCallingUid(), context.getPackageName());
            Log.e("399", " property: " + property);

            if (AppOpsManager.MODE_ALLOWED == property) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        Log.e("399", "Below API 19 cannot invoke!");
    }
    return false;
}



/**
 * 请求用户给予悬浮窗的权限
 */
public void requestPermission(Context context) {
    if (!isFloatWindowOpAllowed(context)) {//已经开启
        openSetting(context);
    }
}


/**
 * 打开权限设置界面
 */
public void openSetting(Context context) {
    try {
        Intent localIntent = new Intent(
                "miui.intent.action.APP_PERM_EDITOR");
        localIntent.setClassName("com.miui.securitycenter",
                "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
        localIntent.putExtra("extra_pkgname", context.getPackageName());
        localIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(localIntent);
        ParachuteLog.d(TAG,"启动小米悬浮窗设置界面");
    } catch (ActivityNotFoundException localActivityNotFoundException) {
        Intent intent1 = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromParts("package", context.getPackageName(), null);
        intent1.setData(uri);
        context.startActivity(intent1);
        ParachuteLog.d(TAG,"启动悬浮窗界面");
    }
    Toast.makeText(context, "请开启应用显示悬浮窗权限!", Toast.LENGTH_SHORT).show();

}


public void showAgreement(Context context, AgreementDialog.AgreementCallback agreementCallback){
    if(context == null){
        return;
    }
    if(!(context instanceof Activity)) {
        if ("Xiaomi".equals(Build.MANUFACTURER)) {//小米手机
            ParachuteLog.d(TAG, "小米手机");
            requestPermission(context);
        } else if ("Meizu".equals(Build.MANUFACTURER)) {//魅族手机
            ParachuteLog.d(TAG, "魅族手机");
            requestPermission(context);
        } else {//其他手机
            ParachuteLog.d(TAG, "其他手机");
            if (Build.VERSION.SDK_INT >= 23) {
                if (!Settings.canDrawOverlays(context)) {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                    Toast.makeText(context, "请开启应用显示悬浮窗权限!", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    String time = SPUtils.get(context, "agreement_showtime", "0");
    ParachuteLog.d(TAG, "  get time =" + time);
    if(System.currentTimeMillis()>Long.valueOf(time)){
        final AgreementDialog dialog = new AgreementDialog(context);
        if(!(context instanceof Activity)){
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        }
        dialog.setCallback(agreementCallback);
        dialog.getWindow().setDimAmount(0.88f);
        dialog.show();
    } else {
        if (agreementCallback != null) {
            String newtime = (String) SPUtils.getOrigin(context, "agreement_showtime", "0");
            boolean agree = (boolean) SPUtils.getOrigin(context, "agreement_agree", false);
            agreementCallback.agreeCallback(false,agree, newtime);

        }

    }


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值