AppLaunchChecker

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51156116


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.
     *
     * <p>To track this state properly you must call {@link #onActivityCreate(Activity)}
     * in your launcher activity's {@link Activity#onCreate(Bundle)} method.</p>
     *
     * @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.
     *
     * <p>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.</p>     *
     *
     * @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~~


博文到此结束!感谢大家支持!谢谢~

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值