Android蓝牙权限的判断

经常要用到,所以记录下来

/**
 * 请求权限的Code
 */
public static final int REQUEST_PERMISSION_CODE = 1500;

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_PERMISSION_CODE) {
        checkPermission();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_PERMISSION_CODE) {
        checkPermission();
    }
}

/**
 * 检查权限
 */
private void checkPermission() {
    // 没有打开蓝牙就请求打开蓝牙
    if (!hasBluetooth()) {
        requestBluetooth();
        return;
    }
    // 没有定位权限就请求定位权限
    if (!hasLocationPermission()) {
        requestLocationPermission(this);
        return;
    }
    // 没有定位服务就请求定位服务
    if (!hasLocationService()) {
        requestLocationService();
        return;
    }
    // 权限都有了,OK
    // TODO
}

/**
 * 是否有定位权限
 *
 * @return boolean
 */
private boolean hasLocationPermission() {
    return ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}

/**
 * 蓝牙是否打开
 *
 * @return boolean
 */
private boolean hasBluetooth() {
    return BluetoothAdapter.getDefaultAdapter().isEnabled();
}

/**
 * 定位服务是否打开
 *
 * @return boolean
 */
private boolean hasLocationService() {
    LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
    if (locationManager == null) {
        return false;
    }
    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}

/**
 * 申请定位权限
 */
private void requestLocationPermission(Activity activity) {
    ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION_CODE);
}

/**
 * 申请打开蓝牙
 */
private void requestBluetooth() {
    Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(intent, REQUEST_PERMISSION_CODE);
}

/**
 * 申请打开定位服务
 */
private void requestLocationService() {
    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivityForResult(intent, REQUEST_PERMISSION_CODE);
}

把博客的搜索框也加上了pjax

之前搜索会导致页面刷新,现在不会了

可以看到,博客现在点击不同的文章,或者搜索文章,背景图片都不会刷新了

js如下

// 搜索使用pjax
$(document).on('submit', '#search', function (event) {
    $.pjax.submit(event, '#pjax_container', {
        fragment:'#pjax_container', timeout:6000
    })
})

Chrome将网站识别为英文

一打开博客,右上角就提示是否要翻译成中文?我本来就是中文站啊

解决方法

head里加上一行

<meta http-equiv="Content-Language" content="zh-CN" />

Typecho迁移到SQLite后,搜索不到内容的坑

迁移之后,我想搜索个nginx相关的文章,结果竟然搜不到,明明标题写着大大的nginx,我就觉得肯定出了问题。

Typecho的搜索代码在 /var/Widget/Archive.php 1185行

$select->where('table.contents.password IS NULL')
->where('table.contents.title LIKE ? OR table.contents.text LIKE ?', $searchQuery, $searchQuery)
->where('table.contents.type = ?', 'post');

看了看发现没有问题,用的是Like,判断标题或内容包含。
type字段是文章类型,独立页面是page,文章是post,所以这里使用post没问题。
那问题就只可能出在第一行table.contents.password IS NULL

- 阅读全文 -

前端大佬帮忙写的随机背景动漫图片

@刀客塔 帮忙写的,放在css里就好了

接口用的是另一个忘了名字的大佬的 https://api.btstu.cn/sjbz/?lx=dongman
哪天不行了换个接口就可以了

/* 随机动漫背景 */
body:before {
    background: url(https://api.btstu.cn/sjbz/?lx=dongman);
    opacity: 0.3;
    content: "";
    display: block;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: -101;
}

Typecho使用pjax后,第一次评论无效的bug

Typecho使用了pjax之后,发现评论点发表,没有任何反应,再发一次评论才有效,在网上找了很久,找到个简单的解决方法。

原址:http://yuanmu.mzzhost.com/tutorials/typecho-pjax-theme-compatible-with-antispam.html?i=1

comments.php
第45行之后,插入一行代码就能解决

<?php if ($this->options->commentsAntiSpam): ?><nocompress><script>(function(){var a=document.addEventListener?{add:"addEventListener",focus:"focus",load:"DOMContentLoaded"}:{add:"attachEvent",focus:"onfocus",load:"onload"};var c,d,e,f,b=document.getElementById("<?php $this->respondId() ?>");null!=b&&(c=b.getElementsByTagName("form"),c.length>0&&(d=c[0],e=d.getElementsByTagName("textarea")[0],f=!1,null!=e&&"text"==e.name&&e[a.add](a.focus,function(){if(!f){var a=document.createElement("input");a.type="hidden",a.name="_",d.appendChild(a),f=!0,a.value=<?php echo Typecho_Common::shuffleScriptVar($this->security->getToken(preg_replace('/\??&?_pjax=[^&]+/i','',$this->request->getRequestUrl()))) ?>}})))})();</script></nocompress><?php endif ?>
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<div id="comments">
    <?php $this->comments()->to($comments); ?>
    <?php if ($comments->have()): ?>
    <h3><?php $this->commentsNum(_t('暂无评论'), _t('仅有一条评论'), _t('已有 %d 条评论')); ?></h3>
    
    <?php $comments->listComments(); ?>

    <?php $comments->pageNav('&laquo; 前一页', '后一页 &raquo;'); ?>
    
    <?php endif; ?>

    <?php if($this->allow('comment')): ?>
    <div id="<?php $this->respondId(); ?>" class="respond">
        <div class="cancel-comment-reply">
        <?php $comments->cancelReply(); ?>
        </div>
    
        <h3 id="response"><?php _e('添加新评论'); ?></h3>
        <form method="post" action="<?php $this->commentUrl() ?>" id="comment-form" role="form">
            <?php if($this->user->hasLogin()): ?>
            <p><?php _e('登录身份: '); ?><a href="<?php $this->options->profileUrl(); ?>"><?php $this->user->screenName(); ?></a>. <a href="<?php $this->options->logoutUrl(); ?>" title="Logout"><?php _e('退出'); ?> &raquo;</a></p>
            <?php else: ?>
            <p>
                <label for="author" class="required"><?php _e('称呼'); ?></label>
                <input type="text" name="author" id="author" class="text" value="<?php $this->remember('author'); ?>" required />
            </p>
            <p>
                <label for="mail"<?php if ($this->options->commentsRequireMail): ?> class="required"<?php endif; ?>><?php _e('Email'); ?></label>
                <input type="email" name="mail" id="mail" class="text" value="<?php $this->remember('mail'); ?>"<?php if ($this->options->commentsRequireMail): ?> required<?php endif; ?> />
            </p>
            <!--<p>
                <label for="url"<?php if ($this->options->commentsRequireURL): ?> class="required"<?php endif; ?>><?php _e('网站'); ?></label>
                <input type="url" name="url" id="url" class="text" placeholder="<?php _e('http://'); ?>" value="<?php $this->remember('url'); ?>"<?php if ($this->options->commentsRequireURL): ?> required<?php endif; ?> />
            </p>-->
            <?php endif; ?>
            <p>
                <label for="textarea" class="required"><?php _e('内容'); ?></label>
                <textarea rows="8" cols="50" name="text" id="textarea" class="textarea" required ><?php $this->remember('text'); ?></textarea>
            </p>
            <p>
                <button type="submit" class="submit"><?php _e('提交评论'); ?></button>
            </p>
        </form>
    </div>
    <?php if ($this->options->commentsAntiSpam): ?><nocompress><script>(function(){var a=document.addEventListener?{add:"addEventListener",focus:"focus",load:"DOMContentLoaded"}:{add:"attachEvent",focus:"onfocus",load:"onload"};var c,d,e,f,b=document.getElementById("<?php $this->respondId() ?>");null!=b&&(c=b.getElementsByTagName("form"),c.length>0&&(d=c[0],e=d.getElementsByTagName("textarea")[0],f=!1,null!=e&&"text"==e.name&&e[a.add](a.focus,function(){if(!f){var a=document.createElement("input");a.type="hidden",a.name="_",d.appendChild(a),f=!0,a.value=<?php echo Typecho_Common::shuffleScriptVar($this->security->getToken(preg_replace('/\??&?_pjax=[^&]+/i','',$this->request->getRequestUrl()))) ?>}})))})();</script></nocompress><?php endif ?>
    <?php else: ?>
    <h3><?php _e('评论已关闭'); ?></h3>
    <?php endif; ?>
</div>