Android 获取activity实例

从外部获取某一个activity实例,通过弱引用的方式:
1,要用到activity实例的类:

public class FCMMessagingService
    private static WeakReference<Activity> activityWeakRef;
    public static void setBaseActivityWeakRef(Activity activity) {
        activityWeakRef = new WeakReference<>(activity);
    }
}

2, activity onCreate()方法中添加实例:

    FCMMessagingService.setBaseActivityWeakRef(this);

3,使用·:

    activityWeakRef.get()

你可能感兴趣的:(android,activity实例,弱引用)