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

AppLaunchChecker

編輯:關於Android編程

 

Android Support Library 23.3.0 是目前發布的最新版本。

主要修復了Support v4 library, AppCompat library, RecyclerView, MediaRouter library, Design Support Library以及v7 Preference library的若干bug。

詳細的更新日志參考官方地址:23.3.0更新日志。

在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有兩種形式,一種是點擊屏幕上的圖標啟動app;另外一種就是通過web界面進行打開app。

通常會對兩種形式做不同的操作。所以就需要判斷是否是從主屏幕上啟動的還是通過別的方式打開的。

package android.support.v4.app;

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

/**
 * This class provides APIs for determining how an app has been launched.
 * This can be useful if you want to confirm that a user has launched your
 * app through its front door activity from their launcher/home screen, rather
 * than just if the app has been opened in the past in order to view a link,
 * open a document or perform some other service for other apps on the device.
 */
public class AppLaunchChecker {
    private static final String SHARED_PREFS_NAME = "android.support.AppLaunchChecker";
    private static final String KEY_STARTED_FROM_LAUNCHER = "startedFromLauncher";

    /**
     * Checks if this app has been launched by the user from their launcher or home screen
     * since it was installed.
     *
     * 

To track this state properly you must call {@link #onActivityCreate(Activity)} * in your launcher activity's {@link Activity#onCreate(Bundle)} method.

* * @param context Context to check * @return true if this app has been started by the user from the launcher at least once */ public static boolean hasStartedFromLauncher(Context context) { return context.getSharedPreferences(SHARED_PREFS_NAME, 0) .getBoolean(KEY_STARTED_FROM_LAUNCHER, false); } /** * Records the parameters of an activity's launch for later use by the other * methods available on this class. * *

Your app should call this method in your launcher activity's * {@link Activity#onCreate(Bundle)} method to track launch state. * If the app targets API 23 (Android 6.0 Marshmallow) or later, this state will be * eligible for full data backup and may be restored to the user's device automatically.

* * * @param activity the Activity currently running onCreate */ 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類型保存到SharedPreference一個boolean類型變量,然後在調用hasStartedFromLauncher()方法的時候去讀取該Boolean值,進行判斷。

但是如果sharedPreference中已經保存了true值,以後每次的hasStartedFromLauncher()判斷都是返回true。

應為在保存變量的時候,它並沒有把不是主屏幕啟動的false放到SharedPreference中

所以個人感覺這個設計不合理。(也許該類的真正用途不在於此。)


最後,本人修改了一下這個工具類,從而可以每次正確的判斷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