Java Code Examples for android.app.Application#registerActivityLifecycleCallbacks()

The following examples show how to use android.app.Application#registerActivityLifecycleCallbacks() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Tracker.java    From Tracker with MIT License 6 votes vote down vote up
public void init(Application context, TrackerConfiguration config) {
	if (config == null) {
		throw new IllegalArgumentException("config can't be null");
	}

	isInit = true;
	this.context = context;
	setTrackerConfig(config);
	preferences = context.getSharedPreferences(context.getPackageName(), MODE_PRIVATE);
	if (preferences.getBoolean(KEY_IS_NEW_DEVICE, true) && !TextUtils.isEmpty(config.getNewDeviceUrl())) {
		submitDeviceInfo();
	}
	context.registerActivityLifecycleCallbacks(new ActivityLifecycleListener());
	if (config.getUploadCategory() == UPLOAD_CATEGORY.REAL_TIME) {
		UploadEventService.enter(context, config.getHostName(), config.getHostPort(), null);
	} else {
		if (!requestConfig) {
			startRequestConfig();
		}
	}
}
 
Example 2
Source File: Tracker.java    From snowplow-android-tracker with Apache License 2.0 5 votes vote down vote up
private void initializeScreenviewTracking() {
    if (this.activityTracking) {
        ActivityLifecycleHandler handler = new ActivityLifecycleHandler();
        Application application = (Application) context.getApplicationContext();
        application.registerActivityLifecycleCallbacks(handler);
    }
}
 
Example 3
Source File: ForegroundFlowableModule.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
@AppForeground
public ConnectableFlowable<String> providesAppForegroundEventStream(Application application) {
  ForegroundNotifier notifier = new ForegroundNotifier();
  ConnectableFlowable<String> foregroundFlowable = notifier.foregroundFlowable();
  foregroundFlowable.connect();

  application.registerActivityLifecycleCallbacks(notifier);
  return foregroundFlowable;
}
 
Example 4
Source File: HeatMapViewCrawler.java    From sa-sdk-android with Apache License 2.0 5 votes vote down vote up
@Override
public void startUpdates() {
    try {
        if (!TextUtils.isEmpty(mFeatureCode) && !TextUtils.isEmpty(mPostUrl)) {
            final Application app = (Application) mActivity.getApplicationContext();
            app.registerActivityLifecycleCallbacks(mLifecycleCallbacks);

            mMessageThreadHandler.start();
            mMessageThreadHandler
                    .sendMessage(mMessageThreadHandler.obtainMessage(MESSAGE_SEND_STATE_FOR_EDITING));
        }
    } catch (Exception e) {
        com.sensorsdata.analytics.android.sdk.SALog.printStackTrace(e);
    }
}
 
Example 5
Source File: AmplitudeClient.java    From shinny-futures-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Enable foreground tracking for the SDK. This is <b>HIGHLY RECOMMENDED</b>, and will allow
 * for accurate session tracking.
 *
 * @param app the Android application
 * @return the AmplitudeClient
 * @see <a href="https://github.com/amplitude/Amplitude-Android#tracking-sessions">
 * Tracking Sessions</a>
 */
public AmplitudeClient enableForegroundTracking(Application app) {
    if (usingForegroundTracking || !contextAndApiKeySet("enableForegroundTracking()")) {
        return this;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        app.registerActivityLifecycleCallbacks(new AmplitudeCallbacks(this));
    }

    return this;
}
 
Example 6
Source File: ActivityMgr.java    From GcmForMojo with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 初始化方法
 * @param app 应用程序
 */
public void init(Application app, Activity initActivity) {
    HMSAgentLog.d("init");

    if (application != null) {
        application.unregisterActivityLifecycleCallbacks(this);
    }

    application = app;
    lastActivity = initActivity;
    app.registerActivityLifecycleCallbacks(this);
}
 
Example 7
Source File: ActivityListenerInit.java    From ucar-weex-core with Apache License 2.0 5 votes vote down vote up
public static synchronized void init(Application application) {
    if (!hasInited) {
        if (Build.VERSION.SDK_INT >= 14) {
            application.registerActivityLifecycleCallbacks(new ActivityLifecycleListener());
        }
        hasInited = true;
    }

}
 
Example 8
Source File: CrashHelper.java    From AndroidCrashHelper with MIT License 5 votes vote down vote up
/**
 * Installs this crash tool on the application using the default error activity.
 *
 * @param context Application to use for obtaining the ApplicationContext. Must not be null.
 * @see Application
 */
public static void install(Context context, Class<? extends Activity> clazz) {
    try {
        if (context == null) {
            Log.e(TAG, "Install failed: context is null!");
            return;
        }
        application = (Application) context.getApplicationContext();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            Log.w(TAG, "Crash tool will be installed, but may not be reliable in API lower than 14");
        }

        //INSTALL!
        Thread.UncaughtExceptionHandler oldHandler = Thread.getDefaultUncaughtExceptionHandler();

        String pkgName = application.getPackageName();
        Log.d(TAG, "current application package name is " + pkgName);
        if (oldHandler != null && oldHandler.getClass().getName().startsWith(pkgName)) {
            Log.e(TAG, "You have already installed crash tool, doing nothing!");
            return;
        }
        if (oldHandler != null && !oldHandler.getClass().getName().startsWith("com.android.internal.os")) {
            Log.e(TAG, "IMPORTANT WARNING! You already have an UncaughtExceptionHandler, are you sure this is correct? If you use ACRA, Crashlytics or similar libraries, you must initialize them AFTER this crash tool! Installing anyway, but your original handler will not be called.");
        }

        //We define a default exception handler that does what we want so it can be called from Crashlytics/ACRA
        Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            application.registerActivityLifecycleCallbacks(new MyActivityLifecycleCallbacks());
        }

        Log.i(TAG, "Crash tool has been installed.");
    } catch (Throwable t) {
        Log.e(TAG, "An unknown error occurred while installing crash tool, it may not have been properly initialized. Please report this as a bug if needed.", t);
    }

    if (clazz != null) {
        setCrashActivityClass(clazz);
    }
}
 
Example 9
Source File: PluginManager.java    From Neptune with Apache License 2.0 5 votes vote down vote up
/**
 * 注册ActivityLifeCycle到插件的Application
 */
public static void registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback) {
    synchronized (sActivityLifecycleCallbacks) {
        sActivityLifecycleCallbacks.add(callback);
    }
    // 对于已经运行的插件,需要注册到其Application类中
    for (Map.Entry<String, PluginLoadedApk> entry : sPluginsMap.entrySet()) {
        PluginLoadedApk loadedApk = entry.getValue();
        if (loadedApk != null && loadedApk.getPluginApplication() != null) {
            Application application = loadedApk.getPluginApplication();
            application.registerActivityLifecycleCallbacks(callback);
        }
    }
}
 
Example 10
Source File: k.java    From letv with Apache License 2.0 4 votes vote down vote up
public static void a(Application application) {
    ActivityLifecycleCallbacks lVar = new l();
    application.unregisterActivityLifecycleCallbacks(lVar);
    application.registerActivityLifecycleCallbacks(lVar);
}
 
Example 11
Source File: ForegroundDetector.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
public ForegroundDetector(Application application) {
    Instance = this;
    application.registerActivityLifecycleCallbacks(this);
}
 
Example 12
Source File: IMMLeaks.java    From diycode with Apache License 2.0 4 votes vote down vote up
/**
 * Fix for https://code.google.com/p/android/issues/detail?id=171190 .
 *
 * When a view that has focus gets detached, we wait for the main thread to be idle and then
 * check if the InputMethodManager is leaking a view. If yes, we tell it that the decor view got
 * focus, which is what happens if you press home and come back from recent apps. This replaces
 * the reference to the detached view with a reference to the decor view.
 *
 * Should be called from {@link Activity#onCreate(android.os.Bundle)} )}.
 */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static void fixFocusedViewLeak(Application application) {

  // Don't know about other versions yet.
  if (SDK_INT < KITKAT || SDK_INT > 22) {
    return;
  }

  final InputMethodManager inputMethodManager =
      (InputMethodManager) application.getSystemService(INPUT_METHOD_SERVICE);

  final Field mServedViewField;
  final Field mHField;
  final Method finishInputLockedMethod;
  final Method focusInMethod;
  try {
    mServedViewField = InputMethodManager.class.getDeclaredField("mServedView");
    mServedViewField.setAccessible(true);
    mHField = InputMethodManager.class.getDeclaredField("mServedView");
    mHField.setAccessible(true);
    finishInputLockedMethod = InputMethodManager.class.getDeclaredMethod("finishInputLocked");
    finishInputLockedMethod.setAccessible(true);
    focusInMethod = InputMethodManager.class.getDeclaredMethod("focusIn", View.class);
    focusInMethod.setAccessible(true);
  } catch (NoSuchMethodException | NoSuchFieldException unexpected) {
    Log.e("IMMLeaks", "Unexpected reflection exception", unexpected);
    return;
  }

  application.registerActivityLifecycleCallbacks(new LifecycleCallbacksAdapter() {
    @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
      ReferenceCleaner cleaner =
          new ReferenceCleaner(inputMethodManager, mHField, mServedViewField,
              finishInputLockedMethod);
      View rootView = activity.getWindow().getDecorView().getRootView();
      ViewTreeObserver viewTreeObserver = rootView.getViewTreeObserver();
      viewTreeObserver.addOnGlobalFocusChangeListener(cleaner);
    }
  });
}
 
Example 13
Source File: DebugViewContainer.java    From u2020-mvp with Apache License 2.0 4 votes vote down vote up
@Override
public ViewGroup forActivity(final Activity activity) {
    activity.setContentView(R.layout.debug_activity_frame);
    final ViewHolder viewHolder = new ViewHolder();
    ButterKnife.bind(viewHolder, activity);

    final Context drawerContext = new ContextThemeWrapper(activity, R.style.Theme_U2020_Debug);
    final DebugView debugView = new DebugView(drawerContext);
    viewHolder.debugDrawer.addView(debugView);

    // Set up the contextual actions to watch views coming in and out of the content area.
    ContextualDebugActions contextualActions = debugView.getContextualDebugActions();
    contextualActions.setActionClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            viewHolder.drawerLayout.closeDrawers();
        }
    });
    viewHolder.content.setOnHierarchyChangeListener(HierarchyTreeChangeListener.wrap(contextualActions));

    viewHolder.drawerLayout.setDrawerShadow(R.drawable.debug_drawer_shadow, GravityCompat.END);
    viewHolder.drawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
        @Override
        public void onDrawerOpened(View drawerView) {
            debugView.onDrawerOpened();
        }
    });

    // If you have not seen the debug drawer before, show it with a message
    if (!seenDebugDrawer.get()) {
        viewHolder.drawerLayout.postDelayed(() -> {
            viewHolder.drawerLayout.openDrawer(GravityCompat.END);
            Toast.makeText(drawerContext, R.string.debug_drawer_welcome, Toast.LENGTH_LONG).show();
        }, 1000);
        seenDebugDrawer.set(true);
    }

    final CompositeSubscription subscriptions = new CompositeSubscription();
    setupMadge(viewHolder, subscriptions);
    setupScalpel(viewHolder, subscriptions);

    final Application app = activity.getApplication();
    app.registerActivityLifecycleCallbacks(new ActivityHierarchyServer.Empty() {
        @Override
        public void onActivityDestroyed(Activity lifecycleActivity) {
            if (lifecycleActivity == activity) {
                subscriptions.unsubscribe();
                app.unregisterActivityLifecycleCallbacks(this);
            }
        }
    });

    riseAndShine(activity);
    return viewHolder.content;
}
 
Example 14
Source File: SwipeBackManager.java    From AndroidProjects with MIT License 4 votes vote down vote up
/**
 * 必须在 Application 的 onCreate 方法中调用
 */
public void init(Application application) {
    application.registerActivityLifecycleCallbacks(this);
}
 
Example 15
Source File: ActivityLifecycleMonitor.java    From mobile-messaging-sdk-android with Apache License 2.0 4 votes vote down vote up
public ActivityLifecycleMonitor(@NonNull Application application) {
    application.registerActivityLifecycleCallbacks(this);
}
 
Example 16
Source File: AppFrontBackHelper.java    From MyBookshelf with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 注册状态监听,仅在Application中使用
 */
public void register(Application application, OnAppStatusListener listener) {
    mOnAppStatusListener = listener;
    application.registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
}
 
Example 17
Source File: Monitor.java    From BehaviorCollect with GNU General Public License v3.0 4 votes vote down vote up
public static void init(@NonNull Application application, boolean collect ) {
	application.registerActivityLifecycleCallbacks(new MonitorActivityLifecycleCallbacks());
	collectMode = collect;
}
 
Example 18
Source File: BackgroundManager.java    From AppCrash with Apache License 2.0 4 votes vote down vote up
private BackgroundManager(Application application) {
    application.registerActivityLifecycleCallbacks(this);
}
 
Example 19
Source File: SkinActivityLifecycle.java    From Android-skin-support with MIT License 4 votes vote down vote up
private SkinActivityLifecycle(Application application) {
    application.registerActivityLifecycleCallbacks(this);
    installLayoutFactory(application);
    SkinCompatManager.getInstance().addObserver(getObserver(application));
}
 
Example 20
Source File: Foreground.java    From com.ruuvi.station with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Its not strictly necessary to use this method - _usually_ invoking
 * get with a Context gives us a path to retrieve the Application and
 * initialise, but sometimes (e.g. in test harness) the ApplicationContext
 * is != the Application, and the docs make no guarantees.
 *
 * @param application
 * @return an initialised Foreground instance
 */
public static Foreground init(Application application){
    if (instance == null) {
        instance = new Foreground();
        application.registerActivityLifecycleCallbacks(instance);
    }
    return instance;
}