gpt4 book ai didi

java - 在 onCreate 方法中传递 Activity 上下文

转载 作者:行者123 更新时间:2023-12-02 12:05:20 24 4
gpt4 key购买 nike

我需要在构造 Activity 后立即将 Activity 上下文传递到我的服务。这是我的代码:

public class myService extends Service
{
private AppCompatActivity activity;

public void setActivity(AppCompatActivity activity)
{
this.activity = activity;
}
}

public class myActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
// ... some things are being executed and myService is being bound
mService.setActivity(this);
}
}

我得到 NullPointerException 因为 - 我想 - myActivity 类仍在构造中并且无法传递引用。如何让Android在onCreate之后运行这个方法?我找到了一些涉及工厂模式的 Java 解决方案,但我不确定如何使用它,如果我可以在我的情况下使用它的话。

最佳答案

Service本身就是一个上下文。因此,如果您只需要 Context,则只需在 Service 类中调用 this 即可。

或者,您应该在启动服务之前将Activity传递给服务。确保在调用 super.onCreate(bundle);

后传递 Activity

但是,您不应该从 Service 操纵您的 Activity 或其 View 。更好的方法是从您的服务通知您的 Activity

Notify activity from service

编辑:观察者模式

创建一个名为NotificationCenter.java的新类

public class NotificationCenter {

private static int totalEvents = 1;

public static final int updateActivity = totalEvents++;
// you can add more events
// public static final int anotherEvent = totalEvents++;

private final SparseArray<ArrayList<Object>> observers = new SparseArray<>();
private final SparseArray<ArrayList<Object>> removeAfterBroadcast = new SparseArray<>();
private final SparseArray<ArrayList<Object>> addAfterBroadcast = new SparseArray<>();

private int broadcasting = 0;

public interface NotificationCenterDelegate {
void didReceivedNotification(int id, Object... args);
}

private static volatile NotificationCenter Instance = null;

public static NotificationCenter getInstance() {
NotificationCenter localInstance = Instance;
if (localInstance == null) {
synchronized (NotificationCenter.class) {
localInstance = Instance;
if (localInstance == null) {
Instance = localInstance = new NotificationCenter();
}
}
}
return localInstance;
}

public void postNotificationName(int id, Object... args) {
broadcasting++;
ArrayList<Object> objects = observers.get(id);
if (objects != null && !objects.isEmpty()) {
for (int a = 0; a < objects.size(); a++) {
Object obj = objects.get(a);
((NotificationCenterDelegate) obj).didReceivedNotification(id, args);
}
}
broadcasting--;
if (broadcasting == 0) {
if (removeAfterBroadcast.size() != 0) {
for (int a = 0; a < removeAfterBroadcast.size(); a++) {
int key = removeAfterBroadcast.keyAt(a);
ArrayList<Object> arrayList = removeAfterBroadcast.get(key);
for (int b = 0; b < arrayList.size(); b++) {
removeObserver(arrayList.get(b), key);
}
}
removeAfterBroadcast.clear();
}
if (addAfterBroadcast.size() != 0) {
for (int a = 0; a < addAfterBroadcast.size(); a++) {
int key = addAfterBroadcast.keyAt(a);
ArrayList<Object> arrayList = addAfterBroadcast.get(key);
for (int b = 0; b < arrayList.size(); b++) {
addObserver(arrayList.get(b), key);
}
}
addAfterBroadcast.clear();
}
}
}

public void addObserver(Object observer, int id) {
if (broadcasting != 0) {
ArrayList<Object> arrayList = addAfterBroadcast.get(id);
if (arrayList == null) {
arrayList = new ArrayList<>();
addAfterBroadcast.put(id, arrayList);
}
arrayList.add(observer);
return;
}
ArrayList<Object> objects = observers.get(id);
if (objects == null) {
observers.put(id, (objects = new ArrayList<>()));
}
if (objects.contains(observer)) {
return;
}
objects.add(observer);
}

public void removeObserver(Object observer, int id) {
if (broadcasting != 0) {
ArrayList<Object> arrayList = removeAfterBroadcast.get(id);
if (arrayList == null) {
arrayList = new ArrayList<>();
removeAfterBroadcast.put(id, arrayList);
}
arrayList.add(observer);
return;
}
ArrayList<Object> objects = observers.get(id);
if (objects != null) {
objects.remove(observer);
}
}
}

然后让您的Activities看起来像这样,您在didReceivedNotification()中从Service接收消息

public class YourActivity implements NotificationCenter.NotificationCenterDelegate {

@Override
public void onPause() {
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.updateActivity);
super.onPause();
}

@Override
public void onResume() {
NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateActivity);
super.onResume();
}

@Override
public void didReceivedNotification(int id, Object... args) {
if (id == NotificationCenter.updateActivity) {
// do something with your activity, your service called this
}
}
}

最后将Service中的消息发送到所有正在监听的Activity:

NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateActivity, optionalData);

这非常好,您不必传递 Activity 实例。

NotificationCenter 来源来自 Telegram。

关于java - 在 onCreate 方法中传递 Activity 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46939955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com