下面是一個示例代碼,可以通過使用自定義覆蓋來禁用Pull通知,並且它可以在下面的版本和6+版本上正常工作。整合後,讓我們知道你是否面臨更多問題。
權限要求清單:
<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
private void disablePullNotificationTouch() {
try {
Log.v("App", "Disable Pull Notification");
private HUDView mView = new HUDView(this);
int statusBarHeight = (int) Math.ceil(25 * getResources().getDisplayMetrics().density);
Log.v("App", "" + statusBarHeight);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
statusBarHeight,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, //Disables status bar
PixelFormat.TRANSPARENT); //Transparent
params.gravity = Gravity.CENTER | Gravity.TOP;
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mView, params);
}catch(Exception e){
Log.v("App","Exception: "+e.getMessage());
}
}
/** code to post/handler request for permission */
public final static int REQUEST_CODE = -1010101;
@RequiresApi(api = Build.VERSION_CODES.M)
public void checkDrawOverlayPermission() {
Log.v("App","Package Name: "+getApplicationContext().getPackageName());
/** check if we already have permission to draw over other apps**/
if (!Settings.canDrawOverlays(context)) {
Log.v("App","Requesting Permission"+Settings.canDrawOverlays(context));
/** if not construct intent to request permission**/
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" +getApplicationContext().getPackageName()));
/* request permission via start activity for result */
startActivityForResult(intent, REQUEST_CODE);
}else{
Log.v("App","We already have permission for it.");
disablePullNotificationTouch();
}
}
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.v("App","OnActivity Result.");
//check if received result code
// is equal our requested code for draw permission
if (requestCode == REQUEST_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.canDrawOverlays(this)) {
disablePullNotificationTouch();
}
}
}
}
修改代碼: 不要忘了使用這些權限:
<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
public class MainActivity extends Activity {
public static final int R_PERM = 123;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Log.v("App","Build Version Greater than or equal to M: "+Build.VERSION_CODES.M);
checkDrawOverlayPermission();
}else{
Log.v("App","OS Version Less than M");
//No need for Permission as less then M OS.
}
if ((CheckPermission(this, Manifest.permission.CAMERA)) &&
(CheckPermission(this, Manifest.permission.READ_PHONE_STATE)) &&
(CheckPermission(this, Manifest.permission.NFC))) {
PermHandling();
} else {
RequestPermission(MainActivity.this, Manifest.permission.CAMERA, R_PERM);
RequestPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE, R_PERM);
RequestPermission(MainActivity.this, Manifest.permission.NFC, R_PERM);
//NewPermHandling();
}
}
private void PermHandling() {
//My app internal parts....
//Here my stuff works...
}
//private void NewPermHandling(){
//}
@Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {
switch (permsRequestCode) {
case R_PERM: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
PermHandling();
} else {
//Toast.makeText(this, "Please Grant Permissions other wise app will close.!", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
public void RequestPermission(Activity thisActivity, String Permission, int Code) {
if (ContextCompat.checkSelfPermission(thisActivity,
Permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Permission)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Permission},
Code);
}
}
}
public final static int REQUEST_CODE = -1010101;
@RequiresApi(api = Build.VERSION_CODES.M)
public void checkDrawOverlayPermission() {
Log.v("App","Package Name: "+getApplicationContext().getPackageName());
/** check if we already have permission to draw over other apps**/
if (!Settings.canDrawOverlays(context)) {
Log.v("App","Requesting Permission"+Settings.canDrawOverlays(context));
/** if not construct intent to request permission**/
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" +getApplicationContext().getPackageName()));
/* request permission via start activity for result */
startActivityForResult(intent, REQUEST_CODE); //It will call onActivityResult Function After you press Yes/No and go Back after giving permission
}else{
Log.v("App","We already have permission for it.");
// disablePullNotificationTouch();
//Do your stuff, we got permission captain
}
}
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.v("App","OnActivity Result.");
//check if received result code
// is equal our requested code for draw permission
if (requestCode == REQUEST_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.canDrawOverlays(this)) {
//Permission Granted by Overlay!!!
//Do your Stuff
}
}
}
}
public boolean CheckPermission(Context context, String Permission) {
if (ContextCompat.checkSelfPermission(context,
Permission) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
}
startActivityForResult會叫onActivityResult如果從一個活動叫這個,而不是從服務。閱讀更多關於它here
爲什麼你需要這個應用程序的權限? –
它詢問屏幕覆蓋權限沒有該應用程序不工作......但是當我嘗試打開它有選項「否」給我的應用程序....如果您已經知道Android 6+的屏幕覆蓋問題爲每個應用程序然後試圖幫助我先生... –