Created
August 25, 2020 08:27
-
-
Save crapthings/cb9e4c3ea92abcd5ffb13ae6e59edfcc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cn.hhjjj.mobpush; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Build; | |
import android.provider.Settings; | |
public class NotificationsUtils { | |
public static boolean isNotificationEnabled(Context context) { | |
return NotificationManagerCompat.from(context.getApplicationContext()).areNotificationsEnabled(); | |
} | |
public static void openPush(Activity activity) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
//这种方案适用于 API 26, 即8.0(含8.0)以上可以用 | |
Intent intent = new Intent(); | |
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS); | |
intent.putExtra(Settings.EXTRA_APP_PACKAGE, activity.getPackageName()); | |
intent.putExtra(Settings.EXTRA_CHANNEL_ID, activity.getApplicationInfo().uid); | |
activity.startActivity(intent); | |
} else { | |
PermissionUtil.toPermissionSetting(activity); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cn.hhjjj.mobpush; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.provider.Settings; | |
import android.app.Activity; | |
import android.content.Intent; | |
public class PermissionUtil { | |
/** | |
* 跳转到权限设置 | |
* | |
* @param activity | |
*/ | |
public static void toPermissionSetting(Activity activity) { | |
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) { | |
toSystemConfig(activity); | |
} else { | |
try { | |
toApplicationInfo(activity); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
toSystemConfig(activity); | |
} | |
} | |
} | |
/** | |
* 应用信息界面 | |
* | |
* @param activity | |
*/ | |
public static void toApplicationInfo(Activity activity) { | |
Intent localIntent = new Intent(); | |
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); | |
localIntent.setData(Uri.fromParts("package", activity.getPackageName(), null)); | |
activity.startActivity(localIntent); | |
} | |
/** | |
* 系统设置界面 | |
* | |
* @param activity | |
*/ | |
public static void toSystemConfig(Activity activity) { | |
try { | |
Intent intent = new Intent(Settings.ACTION_SETTINGS); | |
activity.startActivity(intent); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment