1. 程式人生 > >那些年踩過華為手機的坑——相同的程式碼不同的效果

那些年踩過華為手機的坑——相同的程式碼不同的效果

因本人一直用著華為榮耀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開發工程師吧!

暫時遇到的就是這麼兩個坑,以後在遇到一些什麼問題會繼續更新歡迎您的關注!