上一章写到了文字转为语音读出来,最近开发中又遇到了关于友盟推送的问题
下面是我自己总结的问题:
现在随着Android 版本越来越高 很多机型都需要适配。最近做推送就遇到了一个关于Android8.0的权限适配的问题,就是像今日头条,微信,QQ那种的效果,这个需要开启应用详情里面的允许通知的按钮。这个人家再我们下载的时候就已经处理了这个问题,自动获取了这个权限。而我们再开发中,必须得要让用户手动授权。
友盟接入Sdk 和一些配置再官网上看着文档正常操作就可以 友盟开发文档
下面贴出我的代码: 第一个就是正常写了一个工具类用来判断Android 8.0 以上通知栏权限的`
/**第一个方法也是用来判断通知栏权限的 但是并不能适配Android8.0 以上的权限 第二个方法是可以判断Android 8.0 以上通知栏权限的/
public class NotificationsUtils {
private static final String CHECK_OP_NO_THROW = “checkOpNoThrow”;
private static final String OP_POST_NOTIFICATION = “OP_POST_NOTIFICATION”;
@SuppressLint("NewApi")
public static boolean isNotificationEnabled(Context context) {
AppOpsManager mAppOps =
(AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
ApplicationInfo appInfo = context.getApplicationInfo();
String pkg = context.getApplicationContext().getPackageName();
int uid = appInfo.uid;
Class appOpsClass = null;
/* Context.APP_OPS_MANAGER */
try {
appOpsClass = Class.forName(AppOpsManager.class.getName());
Method checkOpNoThrowMethod =
appOpsClass.getMethod(CHECK_OP_NO_THROW,
Integer.TYPE, Integer.TYPE, String.class);
Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
int value = (Integer) opPostNotificationValue.get(Integer.class);
return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) ==
AppOpsManager.MODE_ALLOWED);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 判断通知栏权限 适配8.0 的手机
* @param context
* @return
*/
public static boolean isEnableV26(Context context) {
ApplicationInfo appInfo = context.getApplicationInfo();
String pkg = context.getApplicationContext().getPackageName();
int uid = appInfo.uid;
try {
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Method sServiceField = notificationManager.getClass().getDeclaredMethod("getService");
sServiceField.setAccessible(true);
Object sService = sServiceField.invoke(notificationManager);
Method method = sService.getClass().getDeclaredMethod("areNotificationsEnabledForPackage"
, String.class, Integer.TYPE);
method.setAccessible(true);
return (boolean) method.invoke(sService, pkg, uid);
} catch (Exception e) {
return true;
}
}
}
然后再你的Application登录页面中使用判断就可以
再你的onResume方法中调用
if (!NotificationsUtils.isEnableV26(this)){
showPopupWindow( R.layout.dialog);
}
//加入handler 是为了防止再加载popupWindow的时候报错
private static Handler handler = new Handler();
@SuppressLint(“ResourceAsColor”)
public void showPopupWindow(final int thisId) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
LayoutInflater inflater = LayoutInflater.from(LoginActivity.this);
View view = inflater.inflate(thisId, null);
final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, false);
popupWindow.setBackgroundDrawable(new ColorDrawable());
popupWindow.setOutsideTouchable(true);
TextView context = (TextView) view.findViewById(R.id.tv_dialog_context);
context.setText(“检测到您没有打开通知权限,是否去打开。否则收不到推送的最新消息”);
TextView mTrue = (TextView) view.findViewById(R.id.btn_confirm);
mTrue.setText(“确定”);
TextView mFalse = (TextView) view.findViewById(R.id.btn_off);
mTrue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
Intent localIntent = new Intent();
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 9) {
localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
localIntent.setData(Uri.fromParts("package", LoginActivity.this.getPackageName(), null));
} else if (Build.VERSION.SDK_INT <= 8) {
localIntent.setAction(Intent.ACTION_VIEW);
localIntent.setClassName("com.android.settings",
"com.android.settings.InstalledAppDetails");
localIntent.putExtra("com.android.settings.ApplicationPkgName",
LoginActivity.this.getPackageName());
}
startActivity(localIntent);
}
});
mFalse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
popupWindow.showAtLocation(LoginActivity.this.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
}
},1000);
}
popupWindow 填充的布局就不用写了吧 这个自己创建一个 写个你喜欢的布局就可以
1501

被折叠的 条评论
为什么被折叠?



