Android 8.0 Notification兼容

@RequiresApi(api = Build.VERSION_CODES.O)
private static Notification.Builder getChannelNotification(Context mContext, String id, String title, String messsage) {
    return new Notification.Builder(mContext, id)
            .setContentTitle(title)
            .setContentText(messsage)
            .setOngoing(true)
            .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
            .setSmallIcon(R.mipmap.ic_launcher)
            .setAutoCancel(true);
}
private static void buildnotify(Context context, String title, String content,
                                String voice, Class classname) {
    boolean isMsgVoice = PreferencesHelper.getBooleanData(PreferenceKey.SET_VOICE);
    boolean isMsgShake = PreferencesHelper.getBooleanData(PreferenceKey.SET_SHAKE);
    if (index > 100) {
        index = 0;
    }
    if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(content)) {
        NotificationManager manager = (NotificationManager) context
                .getSystemService(NOTIFICATION_SERVICE);
        Notification noti;
        ++index;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(String.valueOf(index), "温馨提醒", NotificationManager.IMPORTANCE_LOW);
            notificationChannel.enableLights(false);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.enableVibration(false);
            if (manager != null) {
                manager.createNotificationChannel(notificationChannel);
            }
            Notification.Builder mBuilderO = getChannelNotification(context,String.valueOf(index),title,content);
            noti = mBuilderO.build();
        }else{
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setContentTitle(title);
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setContentText(content);
            Intent intent = new Intent(context, classname);
            PendingIntent pi = PendingIntent.getActivity(context, 10, intent, 0);
            builder.setContentIntent(pi);
            if (isMsgShake) {
                long[] pattern = {10, 10, 20};
                builder.setVibrate(pattern);
            }
            noti = builder.build();
        }

        if (isMsgShake) {
            noti.defaults = Notification.DEFAULT_VIBRATE;
        }
        noti.flags = Notification.FLAG_AUTO_CANCEL;
        if (manager != null) {
            manager.notify(index, noti);
        }
        if (isMsgVoice) {
            SysUtils.ring(context, voice, index);
        }
    }
}

OkHttp3 上传basic auth信息

  public OkHttpClient buildBasicAuthClient(final String name, final String password) {
        return new OkHttpClient.Builder().authenticator(new Authenticator() {
            @Override
            public Request authenticate(Route route, Response response) throws IOException {
                String credential = Credentials.basic(name, password);
                return response.request().newBuilder().header("Authorization", credential).build();
            }
        }).build();
    }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值