下面列出了android.provider.Settings#canDrawOverlays ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* request overlay permission when api >= 23
* @param context
* @return
*/
private boolean overlayPermRequest(Context context) {
boolean permNeeded = false;
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (!Settings.canDrawOverlays(context))
{
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + context.getPackageName()));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
permNeeded = true;
}
}
return permNeeded;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Check if the application has draw over other apps permission or not?
//This permission is by default available for API<23. But for API > 23
//you have to ask for the permission in runtime.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
//If the draw over permission is not available open the settings screen
//to grant the permission.
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
} else {
initializeView();
}
}
@Around("activityOnCreatedCall()")
public Object injectDataInspector(ProceedingJoinPoint joinPoint) throws Throwable {
Log.d(DataInspector.TAG, "injectDataInspector");
Context context = (Context) joinPoint.getThis();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.canDrawOverlays(context)) {
dataInspector.onCreate(context);
isRestarting = false;
} else {
isRequestingOverlayPermission = true;
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
((Activity) context).startActivityForResult(intent, OVERLAY_PERMISSION_CALL);
}
} else {
dataInspector.onCreate(context);
}
return joinPoint.proceed();
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (mode == MODE_DRAG) {
return false;
}
this.view = view;
this.position = position;
this.tempPosition = position;
mX = mWindowX - view.getLeft() - this.getLeft();
mY = mWindowY - view.getTop() - this.getTop();
// 如果是Android 6.0 要动态申请权限
if (Build.VERSION.SDK_INT >= 23) {
if (Settings.canDrawOverlays(getContext())) {
initWindow();
} else {
// 跳转到悬浮窗权限管理界面
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
getContext().startActivity(intent);
}
} else {
// 如果小于Android 6.0 则直接执行
initWindow();
}
return true;
}
public static void open(Context context, Uri url){
// Check if Android M or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(context)) {
new AlertDialog.Builder(context)
.setTitle("Need Permissions")
.setMessage("dingo needs \"Draw over other apps\" permission to play videos")
.setCancelable(false)
.setPositiveButton("Give Permission", (dialog, which) -> ContextCompat.startActivity(
context,
new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + context.getPackageName()))
,null))
.show();
}else{
if(isMyServiceRunning(context, PIPVideoPlayerService.class)){
PIPVideoPlayerService.load(url.toString());
}else{
final Intent serviceStarterIntent = new Intent(context, PIPVideoPlayerService.class);
serviceStarterIntent.setData(url);
ContextCompat.startForegroundService(context, serviceStarterIntent);
}
}
}
public void onCreate(Bundle state) {
super.onCreate(state);
if (Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) {
if (Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(this)) {
startActivityForResult(
new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())),
REQUEST_CODE_WINDOW_OVERLAY_PERMISSION);
} else {
onSuccess();
}
} else {
Log.w(TAG, "CreateShortcutActivity called with unexpected Action " + getIntent().getAction());
onFailure();
}
}
/**
* 将当前实例添加到队列{@link SweetToastManager#queue}中,若队列为空,则加入队列后直接进行展示
*/
public void show(){
try {
if (Build.VERSION.SDK_INT >= 23) {
//Android6.0以上,需要动态声明权限
if(mContentView!=null && !Settings.canDrawOverlays(mContentView.getContext().getApplicationContext())) {
//用户还未允许该权限
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
mContentView.getContext().startActivity(intent);
return;
} else if(mContentView!=null) {
//用户已经允许该权限
SweetToastManager.show(this);
}
} else {
//Android6.0以下,不用动态声明权限
if (mContentView!=null) {
SweetToastManager.show(this);
}
}
// SweetToastManager.show(this);
}catch (Exception e){
Log.e("幻海流心","e:"+e.getLocalizedMessage());
}
}
/**
* @return true if {@link android.Manifest.permission#SYSTEM_ALERT_WINDOW} is granted
*/
public static boolean isSystemAlertGranted(@NonNull Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return Settings.canDrawOverlays(context);
}
return true;
}
@SuppressLint("WorldReadableFiles")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSettings = PreferenceManager.getDefaultSharedPreferences(this);
//Setting the theme
if (isDayTheme(this)) {
setTheme(R.style.DayTheme);
}
setContentView(R.layout.activity_main);
checkAndCallPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Toast.makeText(this, R.string.cant_open_popup, Toast.LENGTH_SHORT).show();
startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse(String.format("package:%s", getPackageName()))));
finish();
}
}
sSharedPreferences = getSharedPreferences("data", Context.MODE_WORLD_READABLE);
dealWithIntent();
keepAlive();
//If it is the first time to run...
if (FirstRun.isFirstRun(this, "app")) {
setFragmentWithoutBack(new WelcomeFragment());
//else if there's no rule...
} else {
setFragmentWithoutBack(new MainFragment());
if (isModuleActive()) {
giveMeFive();
} else {
checkModuleActivated();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
if (!Settings.canDrawOverlays(this)) {
TopScrollHelper.getInstance(getApplicationContext()).addTargetScrollView(mNestedScrollView);
}
}
}
}
private boolean hasOverlayPermission() {
if (!isOverlayApiDeprecated()) {
return true; // permission no required
} else {
return Settings.canDrawOverlays(app);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("KCA-MPS", "onStartCommand");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !Settings.canDrawOverlays(getApplicationContext())) {
// Can not draw overlays: pass
stopSelf();
} else if (intent != null && intent.getAction() != null) {
if (intent.getAction().equals(FCHK_SHOW_ACTION)) {
portdeckdata = dbHelper.getJsonArrayValue(DB_KEY_DECKPORT);
if (portdeckdata != null) {
deck_cnt = portdeckdata.size();
setFchkFleetBtnColor(recent_no, deck_cnt);
setFchkFuncBtnColor(current_func);
setText();
}
mView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
popupWidth = mView.getMeasuredWidth();
popupHeight = mView.getMeasuredHeight();
mParams.x = (screenWidth - popupWidth) / 2;
mParams.y = (screenHeight - popupHeight) / 2;
mManager.updateViewLayout(mView, mParams);
}
}
return super.onStartCommand(intent, flags, startId);
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 10) {
if (Settings.canDrawOverlays(this)) {
startWindowService();
} else {
Toast.makeText(WindowManagerActivity.this, "not granted", Toast.LENGTH_SHORT).show();
}
}
}
private static boolean permissionCheck(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Get permission to show debug overlay in dev builds.
if (!Settings.canDrawOverlays(context)) {
// overlay permission not yet granted
return false;
} else {
return true;
}
}
// on pre-M devices permission needs to be specified in manifest
return hasPermission(context, Manifest.permission.SYSTEM_ALERT_WINDOW);
}
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (Settings.canDrawOverlays(this)) {
floaty.startService();
} else {
Spanned message = Html.fromHtml("Please allow this permission, so <b>Floaties</b> could be drawn.");
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_WINDOW_OVERLAY_PERMISSION) {
if (android.os.Build.VERSION.SDK_INT < 23 || Settings.canDrawOverlays(this)) {
onSuccess();
} else {
onFailure();
}
}
}
@TargetApi(Build.VERSION_CODES.M) public static boolean isSystemAlertGranted(@NonNull Context context) {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.canDrawOverlays(context);
}
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !Settings.canDrawOverlays(getApplicationContext())) {
// Can not draw overlays: pass
stopSelf();
} else {
active = true;
clickcount = 0;
dbHelper = new KcaDBHelper(getApplicationContext(), null, KCANOTIFY_DB_VERSION);
deckInfoCalc = new KcaDeckInfo(getApplicationContext(), getBaseContext());
KcaApiData.setDBHelper(dbHelper);
setDefaultGameData();
LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = mInflater.inflate(R.layout.view_labinfo_view, null);
itemView = mInflater.inflate(R.layout.view_battleview_items, null);
mView.setOnTouchListener(mViewTouchListener);
mView.findViewById(R.id.view_lab_head).setOnTouchListener(mViewTouchListener);
mView.setVisibility(View.GONE);
((TextView) mView.findViewById(R.id.view_lab_title)).setText(getStringWithLocale(R.string.viewmenu_airbase_title));
mView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
popupWidth = mView.getMeasuredWidth();
popupHeight = mView.getMeasuredHeight();
// Button (Fairy) Settings
mParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
getWindowLayoutType(),
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
mParams.gravity = Gravity.TOP | Gravity.START;
Display display = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
Log.e("KCA", "w/h: " + String.valueOf(screenWidth) + " " + String.valueOf(screenHeight));
mParams.x = (screenWidth - popupWidth) / 2;
mParams.y = (screenHeight - popupHeight) / 2;
mManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mManager.addView(mView, mParams);
itemViewParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
getWindowLayoutType(),
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
}
}
public static boolean canDrawOverlays(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return Settings.canDrawOverlays(context);
}
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getUseDeveloperSupport() && Build.VERSION.SDK_INT >= 23) {
// Get permission to show redbox in dev builds.
if (!Settings.canDrawOverlays(this)) {
Intent serviceIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
startActivity(serviceIntent);
FLog.w(ReactConstants.TAG, REDBOX_PERMISSION_MESSAGE);
Toast.makeText(this, REDBOX_PERMISSION_MESSAGE, Toast.LENGTH_LONG).show();
}
}
mReactRootView = ReactPreLoader.getRootView(getReactInfo());
if (mReactRootView != null) {
Log.i(TAG, "use pre-load view");
MutableContextWrapper contextWrapper = (MutableContextWrapper) mReactRootView.getContext();
contextWrapper.setBaseContext(this);
try {
ViewGroup viewGroup = (ViewGroup) mReactRootView.getParent();
if (viewGroup != null) {
viewGroup.removeView(mReactRootView);
}
} catch (Exception exception) {
Log.e(TAG, "getParent error", exception);
}
} else {
Log.i(TAG, "createRootView");
mReactRootView = createRootView();
if (mReactRootView != null) {
mReactRootView.startReactApplication(
getReactNativeHost().getReactInstanceManager(),
getMainComponentName(),
getLaunchOptions());
}
}
setContentView(mReactRootView);
mDoubleTapReloadRecognizer = new DoubleTapReloadRecognizer();
}