android.app.Application#registerActivityLifecycleCallbacks ( )源码实例Demo

下面列出了android.app.Application#registerActivityLifecycleCallbacks ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Tracker   文件: Tracker.java
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();
		}
	}
}
 
源代码2 项目: AndroidCrashHelper   文件: CrashHelper.java
/**
 * 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);
    }
}
 
源代码3 项目: sa-sdk-android   文件: HeatMapViewCrawler.java
@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);
    }
}
 
源代码4 项目: shinny-futures-android   文件: AmplitudeClient.java
/**
 * 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;
}
 
源代码5 项目: Neptune   文件: PluginManager.java
/**
 * 注册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);
        }
    }
}
 
源代码6 项目: ucar-weex-core   文件: ActivityListenerInit.java
public static synchronized void init(Application application) {
    if (!hasInited) {
        if (Build.VERSION.SDK_INT >= 14) {
            application.registerActivityLifecycleCallbacks(new ActivityLifecycleListener());
        }
        hasInited = true;
    }

}
 
源代码7 项目: GcmForMojo   文件: ActivityMgr.java
/**
 * 初始化方法
 * @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);
}
 
@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;
}
 
源代码9 项目: snowplow-android-tracker   文件: Tracker.java
private void initializeScreenviewTracking() {
    if (this.activityTracking) {
        ActivityLifecycleHandler handler = new ActivityLifecycleHandler();
        Application application = (Application) context.getApplicationContext();
        application.registerActivityLifecycleCallbacks(handler);
    }
}
 
private SkinActivityLifecycle(Application application) {
    application.registerActivityLifecycleCallbacks(this);
    installLayoutFactory(application);
    SkinCompatManager.getInstance().addObserver(getObserver(application));
}
 
源代码11 项目: AppCrash   文件: BackgroundManager.java
private BackgroundManager(Application application) {
    application.registerActivityLifecycleCallbacks(this);
}
 
源代码12 项目: BehaviorCollect   文件: Monitor.java
public static void init(@NonNull Application application, boolean collect ) {
	application.registerActivityLifecycleCallbacks(new MonitorActivityLifecycleCallbacks());
	collectMode = collect;
}
 
源代码13 项目: MyBookshelf   文件: AppFrontBackHelper.java
/**
 * 注册状态监听,仅在Application中使用
 */
public void register(Application application, OnAppStatusListener listener) {
    mOnAppStatusListener = listener;
    application.registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
}
 
public ActivityLifecycleMonitor(@NonNull Application application) {
    application.registerActivityLifecycleCallbacks(this);
}
 
源代码15 项目: letv   文件: k.java
public static void a(Application application) {
    ActivityLifecycleCallbacks lVar = new l();
    application.unregisterActivityLifecycleCallbacks(lVar);
    application.registerActivityLifecycleCallbacks(lVar);
}
 
源代码16 项目: AndroidProjects   文件: SwipeBackManager.java
/**
 * 必须在 Application 的 onCreate 方法中调用
 */
public void init(Application application) {
    application.registerActivityLifecycleCallbacks(this);
}
 
源代码17 项目: u2020-mvp   文件: DebugViewContainer.java
@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;
}
 
源代码18 项目: diycode   文件: IMMLeaks.java
/**
 * 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);
    }
  });
}
 
源代码19 项目: Telegram   文件: ForegroundDetector.java
public ForegroundDetector(Application application) {
    Instance = this;
    application.registerActivityLifecycleCallbacks(this);
}
 
源代码20 项目: com.ruuvi.station   文件: Foreground.java
/**
 * 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;
}