Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

AppLaunchChecker:判斷 App 是否從桌面啟動

編輯:關於 Android 開發

AppLaunchChecker:判斷 App 是否從桌面啟動

Android Support Library 23.3.0 是當時發布的最新版本,主要修復了 Support v4、AppCompat、RecyclerView、MediaRouter、Design Support Library 以及 v7 Preference 等庫的若干 bug,詳細更新日誌可參考官方地址。

在 Support v4 包中新增了一個 API:AppLaunchChecker。


先看看這個類。官方的說法是:它可以查看 app 過去是否已被啟動過,通過 hasStartedFromLauncher() 方法判斷當前啟動是否由 home screen 觸發。

使用很簡單,在啟動 activity 的 onCreate() 裡調用如下靜態方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // AppLaunchChecker.onActivityCreate(this);
}

然後在其他你想判斷的地方進行判斷即可:

@Override
protected void onResume() {
    super.onResume();
    if (AppLaunchChecker.hasStartedFromLauncher(this)) {
        Toast.makeText(this, "it's started from launcher", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "it's not started from launcher", Toast.LENGTH_SHORT).show();
    }
}

用法就這麼簡單。


那麼這個工具類有什麼用?我們平時啟動 app 有兩種形式:一種是點擊屏幕上的圖標啟動;另一種是通過 web 界面打開 app。通常我們會對兩種形式做不同處理,因此需要判斷究竟是不是從主屏幕啟動的。

看下它的源碼:

package android.support.v4.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.content.IntentCompat;
import android.support.v4.content.SharedPreferencesCompat;

/**
 * This class provides APIs for determining how an app has been launched.
 */
public class AppLaunchChecker {
    private static final String SHARED_PREFS_NAME = "android.support.AppLaunchChecker";
    private static final String KEY_STARTED_FROM_LAUNCHER = "startedFromLauncher";

    public static boolean hasStartedFromLauncher(Context context) {
        return context.getSharedPreferences(SHARED_PREFS_NAME, 0)
                .getBoolean(KEY_STARTED_FROM_LAUNCHER, false);
    }

    public static void onActivityCreate(Activity activity) {
        final SharedPreferences sp = activity.getSharedPreferences(SHARED_PREFS_NAME, 0);
        if (sp.getBoolean(KEY_STARTED_FROM_LAUNCHER, false)) {
            return;
        }
        final Intent launchIntent = activity.getIntent();
        if (launchIntent == null) {
            return;
        }
        if (Intent.ACTION_MAIN.equals(launchIntent.getAction())
                && (launchIntent.hasCategory(Intent.CATEGORY_LAUNCHER)
                    || launchIntent.hasCategory(IntentCompat.CATEGORY_LEANBACK_LAUNCHER))) {
            SharedPreferencesCompat.EditorCompat.getInstance().apply(
                    sp.edit().putBoolean(KEY_STARTED_FROM_LAUNCHER, true));
        }
    }
}

從源碼不難看出,onActivityCreate() 的邏輯是:判斷 Intent 的 action 和 category,若符合桌面啟動特徵,就往 SharedPreferences 裡存一個 true;而 hasStartedFromLauncher() 只是去讀這個布爾值。

但問題在於:一旦 SharedPreference 裡存了 true,此後每次 hasStartedFromLauncher() 都會返回 true。因為保存變量時,它並沒有把"並非從主屏幕啟動"的 false 寫回去。所以個人覺得這個設計不太合理(也許該類的真正用途不在於此)。


最後,我改了一版這個工具類,讓它能每次正確判斷 app 究竟是從主屏幕啟動,還是其他方式啟動的。代碼如下:

public class AppLaunchChecker {
    private static final String SHARED_PREFS_NAME = "android.support.AppLaunchChecker";
    private static final String KEY_STARTED_FROM_LAUNCHER = "startedFromLauncher";

    public static boolean hasStartedFromLauncher(Context context) {
        return context.getSharedPreferences(SHARED_PREFS_NAME, 0)
                .getBoolean(KEY_STARTED_FROM_LAUNCHER, false);
    }

    public static void onActivityCreate(Activity activity) {
        final SharedPreferences sp = activity.getSharedPreferences(SHARED_PREFS_NAME, 0);
        final Intent launchIntent = activity.getIntent();
        if (launchIntent == null) {
            return;
        }
        if (Intent.ACTION_MAIN.equals(launchIntent.getAction())
                && (launchIntent.hasCategory(Intent.CATEGORY_LAUNCHER)
                    || launchIntent.hasCategory(IntentCompat.CATEGORY_LEANBACK_LAUNCHER))) {
            SharedPreferencesCompat.EditorCompat.getInstance().apply(
                    sp.edit().putBoolean(KEY_STARTED_FROM_LAUNCHER, true));
        } else {
            SharedPreferencesCompat.EditorCompat.getInstance().apply(
                    sp.edit().putBoolean(KEY_STARTED_FROM_LAUNCHER, false));
        }
    }
}

類名、方法名都保持一致,使用方式也一樣,so easy~~


博文到此結束,感謝大家支持,謝謝~

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved