安卓,悬浮窗权限动态请求(SYSTEM_ALERT_WINDOW)
悬浮窗权限请求,⽰例:
package com.sc.demo;
t.Intent;
import android.os.Bundle;
import com.sc.permission.FloatTool;
import android.app.Activity;
import com.sci.androidpermission.R;
/**
* 权限请求⽰例
*/
public class MainActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
// this.setContentView(R.layout.activity_main);
FloatTool.RequestOverlayPermission(this);
}
/** Activity执⾏结果 */
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
}
}
package com.sc.permission;
安卓intent用法import android.app.Activity;
import android.app.AlertDialog;
t.DialogInterface;
t.Intent;
import android.Uri;
import android.os.Build;
import android.provider.Settings;
* FloatTool.java:应⽤悬浮窗权限请求
*
* l中添加: <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
*
* ⽤法:
* 1、请求悬浮窗权限:FloatTool.RequestOverlayPermission(this);
* 2、处理悬浮窗权限请求结果:ActivityResult(requestCode, resultCode, data, this);
* -----
* 2019-9-19 下午3:10:34
* scimence
*/
public class FloatTool
{
public static boolean CanShowFloat = false;
private static final int REQUEST_OVERLAY = 5004;
/** 动态请求悬浮窗权限 */
public static void RequestOverlayPermission(Activity Instatnce)
{
if (Build.VERSION.SDK_INT >= 23)
{
if (!Settings.canDrawOverlays(Instatnce))
{
String ACTION_MANAGE_OVERLAY_PERMISSION = "android.settings.action.MANAGE_OVERLAY_PERMISSION";
Intent intent = new Intent(ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + PackageName()));
Instatnce.startActivityForResult(intent, REQUEST_OVERLAY);
}
else
{
CanShowFloat = true;
}
}
}
/** 浮窗权限请求,Activity执⾏结果,回调函数 */
public static void onActivityResult(int requestCode, int resultCode, Intent data, final Activity Instatnce)
{
// Toast.makeText(activity, "onActivityResult设置权限!", Toast.LENGTH_SHORT).show();
if (requestCode == REQUEST_OVERLAY)  // 从应⽤权限设置界⾯返回
{
if(resultCode == Activity.RESULT_OK)
{
CanShowFloat = true;  // 设置标识为可显⽰悬浮窗
}
else
{
CanShowFloat = false;
if (!Settings.canDrawOverlays(Instatnce)) // 若当前未允许显⽰悬浮窗,则提⽰授权
{
AlertDialog.Builder builder = new AlertDialog.Builder(Instatnce);
builder.setCancelable(false);
builder.setTitle("悬浮窗权限未授权");
builder.setMessage("应⽤需要悬浮窗权限,以展⽰浮标");
builder.setPositiveButton("去添加权限", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
RequestOverlayPermission(Instatnce);
});
builder.setNegativeButton("拒绝则退出", new DialogInterface.OnClickListener()    {
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
// 若拒绝了所需的权限请求,则退出应⽤
Instatnce.finish();
}
});
builder.show();
}
}
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。