项目作者: lizhangqu

项目描述 :
Global Application Getter Support, Env Support, Lifecycle Support And Route Support
高级语言: Java
项目地址: git://github.com/lizhangqu/support-application.git
创建时间: 2017-07-06T13:56:46Z
项目社区:https://github.com/lizhangqu/support-application

开源协议:BSD 3-Clause "New" or "Revised" License

下载


Support-Application

  • support-application is a library which can get the information about the app like application, applicationContext, classloader, appName, versionName, versionCode, isDebugAble without context.
  • support-application is a library which can open the dest activity like url.
  • support-application is a library which can control the app develop environment globally.
  • support-application is a library which can get the application lifecycle(foreground/background), the activity lifecycle(onCreated/onStarted/onStopped/onDestroyed), and the top activity in activity stack etc.
  • each class in support-application is independent. so you need init each of them in application method named onCreate if needed.

Changelog

See details in CHANGELOG.

Examples

I have provided a sample.

See sample here on Github.

To run the sample application, simply clone this repository and use android studio to compile, install it on a connected device.

Usage

Dependency

latest version

Download

gradle

  1. dependencies {
  2. //noinspection GradleCompatible
  3. compile "com.android.support:support-application:${latest_version}"
  4. }

maven

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.android.support</groupId>
  4. <artifactId>support-application</artifactId>
  5. <version>${latest_version}</version>
  6. </dependency>
  7. </dependencies>

ApplicationCompat

ApplicationCompat can get the information about the app like application, applicationContext, classloader, appName, versionName, versionCode, isDebugAble without context.

get application

  1. Application application = ApplicationCompat.getApplication();

get application context

  1. Context context = ApplicationCompat.getApplicationContext();

get classloader

  1. ClassLoader classLoader = ApplicationCompat.getClassLoader();

get the apk appName

  1. String appName = ApplicationCompat.getAppName();

get the apk versionName

  1. String versionName = ApplicationCompat.getVersionName();

Get The Apk VersionCode

  1. int versionCode = ApplicationCompat.getVersionCode();

debuggable or not

  1. boolean isDebuggable = ApplicationCompat.isDebuggable();

RouteCompat

RouteCompat can open the Activity like open the Url.

declare url information at intent-filter node in AndroidManifest.xml

  1. <activity android:name=".SecondActivity">
  2. <intent-filter android:priority="999">
  3. <action android:name="android.intent.action.VIEW"></action>
  4. <category android:name="android.intent.category.DEFAULT"></category>
  5. <category android:name="android.intent.category.BROWSABLE"></category>
  6. <data android:scheme="http"></data>
  7. <data android:scheme="https"></data>
  8. <data android:host="support.android.com"></data>
  9. <data android:path="/support/application/second"></data>
  10. </intent-filter>
  11. </activity>

open the activity in code like this

  1. RouteUri routeUri = RouteUri.scheme("https")
  2. .host("support.android.com")
  3. .path("support/application/second")
  4. .param("key1", "value1")
  5. .fragment("1");
  6. RouteCompat.from(MainActivity.this).toUri(routeUri);

get the url information in dest activity

  1. Intent intent = getIntent();
  2. if (intent != null) {
  3. Uri data = getIntent().getData();
  4. if (data != null) {
  5. String host = data.getHost();
  6. String path = data.getPath();
  7. String param = data.getQueryParameter("key1");
  8. String fragment = data.getFragment();
  9. Log.e("TAG", "host:" + host);
  10. Log.e("TAG", "path:" + path);
  11. Log.e("TAG", "param:" + param);
  12. Log.e("TAG", "fragment:" + fragment);
  13. }
  14. }

see more api in RouteCompat.java

EnvironmentCompat

EnvironmentCompat can control the app’s develop environment globally.

init in application method named onCreate

  1. EnvironmentCompat.getInstance().onApplicationCreate(this, EnvironmentCompat.Env.RELEASE);

or use Env.RELEASE for default

  1. EnvironmentCompat.getInstance().onApplicationCreate(this);

get the env in app anywhere if you need

  1. EnvironmentCompat.Env env = EnvironmentCompat.getInstance().getEnv();

change the env if you need

  1. String[] environments = {
  2. EnvironmentCompat.Env.RELEASE.name(),
  3. EnvironmentCompat.Env.PRE.name(),
  4. EnvironmentCompat.Env.DEVELOP.name()
  5. };
  6. EnvironmentCompat.Env env = EnvironmentCompat.getInstance().getEnv();
  7. int ordinal = env.ordinal();
  8. AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
  9. .setTitle("Please Select Environment")
  10. .setCancelable(true)
  11. .setSingleChoiceItems(environments, ordinal, new DialogInterface.OnClickListener() {
  12. @Override
  13. public void onClick(DialogInterface dialog, int which) {
  14. EnvironmentCompat.Env env = EnvironmentCompat.Env.values()[which];
  15. EnvironmentCompat.getInstance().changeEnv(getApplicationContext(), env);
  16. dialog.dismiss();
  17. }
  18. })
  19. .create();
  20. dialog.show();

LifecycleCompat

LifecycleCompat has these features

  • get the activity lifecycle changed callback(onCreated/onStarted/onStopped/onDestroyed)
  • get the application lifecycle changed broadcast(foreground/background)
  • get the activity list count changed broadcast
  • get the top activity in activity stack
  • get launchedActivity count
  • get launchedActivity list
  • show dest activity in activity stack

init in application method named onCreate

  1. LifecycleCompat.getInstance().onApplicationCreate(this);

get the top activity, launchedActivity count, launchedActivity list

  1. int launchedActivityCount = LifecycleCompat.getInstance().getLaunchedActivityCount();
  2. Activity topActivity = LifecycleCompat.getInstance().getTopActivity();
  3. List<WeakReference<Activity>> launchedActivityList = LifecycleCompat.getInstance().getLaunchedActivityList();
  4. Log.e("TAG", "launchedActivityCount:" + launchedActivityCount);
  5. Log.e("TAG", "topActivity:" + topActivity);
  6. if (launchedActivityList != null) {
  7. for (WeakReference<Activity> activityWeakReference : launchedActivityList) {
  8. if (activityWeakReference != null && activityWeakReference.get() != null) {
  9. Log.e("TAG", "launchedActivity:" + activityWeakReference.get());
  10. }
  11. }
  12. }

show dest activity in activity stack

  1. LifecycleCompat.getInstance().showActivity(MainActivity.class);

or use the class name

  1. LifecycleCompat.getInstance().showActivity("com.android.support.application.sample.MainActivity");

register/unregister activity lifecycle changed callback

  1. LifecycleCompat.LifecycleCallback lifecycleCallback = new LifecycleCompat.LifecycleCallback() {
  2. @Override
  3. public void onCreated(Activity activity) {
  4. Log.e("TAG", "onCreated:" + activity);
  5. }
  6. @Override
  7. public void onDestroyed(Activity activity) {
  8. Log.e("TAG", "onDestroyed:" + activity);
  9. }
  10. @Override
  11. public void onStarted(Activity activity) {
  12. Log.e("TAG", "onStarted:" + activity);
  13. }
  14. @Override
  15. public void onStopped(Activity activity) {
  16. Log.e("TAG", "onStopped:" + activity);
  17. }
  18. };
  19. LifecycleCompat.getInstance().registerActivityLifecycleCallback(lifecycleCallback);

if you need to unregister it, you need to call the method named unregisterActivityLifecycleCallback.

  1. LifecycleCompat.getInstance().unregisterActivityLifecycleCallback(lifecycleCallback);

get the application lifecycle changed broadcast

  1. private class ApplicationLifecycleChangedBroadcastReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. boolean isBackground = intent.getBooleanExtra(LifecycleCompat.EXTRA_LIFECYCLE_STATUS, false);
  5. String packageName = intent.getStringExtra(LifecycleCompat.EXTRA_PACKAGE_NAME);
  6. //judge packageName equal
  7. if (TextUtils.equals(packageName, getApplicationContext().getPackageName())) {
  8. if (isBackground) {
  9. Log.e("TAG", "app is background");
  10. } else {
  11. Log.e("TAG", "app is foreground");
  12. }
  13. }
  14. }
  15. }
  16. private ApplicationLifecycleChangedBroadcastReceiver mApplicationLifecycleChangedBroadcastReceiver = new ApplicationLifecycleChangedBroadcastReceiver();
  17. //register broadcast receiver
  18. registerReceiver(mApplicationLifecycleChangedBroadcastReceiver, new IntentFilter(LifecycleCompat.ACTION_APPLICATION_LIFECYCLE_CHANGED));
  19. //unregister broadcast receiver
  20. unregisterReceiver(mApplicationLifecycleChangedBroadcastReceiver);

get the activity list count changed broadcast

  1. private class ActivityCountChangeBroadcastReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. String packageName = intent.getStringExtra(LifecycleCompat.EXTRA_PACKAGE_NAME);
  5. int activityCount = intent.getIntExtra(LifecycleCompat.EXTRA_ACTIVITY_COUNT, -1);
  6. //judge packageName equal
  7. if (TextUtils.equals(packageName, getApplicationContext().getPackageName())) {
  8. Log.e("TAG", "activityCountChanged:" + activityCount);
  9. }
  10. }
  11. }
  12. private ActivityCountChangeBroadcastReceiver mActivityCountChangeBroadcastReceiver = new ActivityCountChangeBroadcastReceiver();
  13. //register broadcast receiver
  14. registerReceiver(mActivityCountChangeBroadcastReceiver, new IntentFilter(LifecycleCompat.ACTION_ACTIVITY_COUNT_CHANGED));
  15. //unregister broadcast receiver
  16. unregisterReceiver(mActivityCountChangeBroadcastReceiver);

get the activity start count and create count

  1. LifecycleCompat.getInstance().getStartedCount();
  2. LifecycleCompat.getInstance().getCreatedCount();

TagCompat

  1. TagCompat.setTag(view, "key1", "value1");
  2. TagCompat.getTag(view, "key1", "defaultValue");
  3. TagCompat.containsTag(view, "key1");

License

support-application is under the BSD license. See the LICENSE file for details.