那些年踩过华为手机的坑——相同的代码不同的效果

来源:互联网 发布:网络战略游戏图片 编辑:程序博客网 时间:2024/06/02 00:09

因本人一直用着华为荣耀6手机,所以也就充当了我的测试机,使用中我深深的体会到了华为手机的各种坑爹之处,由于我大脑记忆细胞有限故整理此篇博客来记录开发中遇到的适配问题…..遇到了新坑会继续更新欢迎关注!

1.HUAWEI Ch2-TL0 启动apk安装程序出错

    /**     *  安装一个apk     *     * @param context 上下文     * @param file    apk路径     */    private void installApk(Context context, File file) {        Intent intent = new Intent();        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");        context.startActivity(intent);    }

通过上面代码来启动一个apk安装,在绝大多数手机上都是没有问题的,连我的HUAWEI H60-L02上都可以正常启动安装。but,HUAWEI Ch2-TL0就无法启动只能打开一个界面。so,搞好了好久只需在增加一行代码就可以,如下:

    /**     * 安装一个apk     *     * @param context 上下文     * @param file    apk路径     */    private void installApk(Context context, File file) {        Intent intent = new Intent();        intent.setAction(Intent.ACTION_VIEW);        //如果加上一行无效果,那么就再加这一行试试        //intent.addCategory(Intent.CATEGORY_DEFAULT);        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");        context.startActivity(intent);    }

2.同样还是HUAWEI Ch2-TL0 点击通知栏的消息,无法启动事先设置好的PendingIntent

    /**     * 显示通知     *     * @param context 上下文     */    private void showNotify(Context context) {        //获取通知管理者        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        Notification.Builder builder = new Notification.Builder(this);        builder.setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher);        Intent intent = new Intent(context, MainActivity.class);        PendingIntent pendIntent = PendingIntent.getActivity(context, (int)                (Math.random() * 1000) + 1, intent, Intent.FLAG_ACTIVITY_CLEAR_TOP);        builder.setContentIntent(pendIntent);        Notification notification = builder.getNotification();        //显示通知消息        manager.notify(110, notification);    }

上面代码重点就在PendingIntent 的设置当中哪些参数的填写。在今天适配之前requestCode和flags这两个参数一直都是乱写的,现在才知道这两个是多么的重要。通知栏这次遇到了两个问题:第一点击不能跳转,第二点击跳转过后Intent携带的数据无法获取到。
点击不能跳转: 需要将requestCode参数填写一个随机数,不然第一次可以跳转第二次就无法跳转了。
跳转过后Intent携带的数据无法获取到: 这个就需要将flag设置如上参数了。

有些参数真心不知道为什么要这么填写,但填写了就能解决当前的适配问题,这里面的具体奥秘还是得去问问HUAWEI的ROM开发工程师吧!

暂时遇到的就是这么两个坑,以后在遇到一些什么问题会继续更新欢迎您的关注!

3 0
原创粉丝点击