我需要在构建活动后立即将活动上下文传递给我的服务。这是我的代码:
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
类仍在构建,并且无法传递引用。如何在onCreate
之后让Android运行此方法?我找到了一些涉及工厂模式的Java解决方案,但我不知道如何在我的情况下使用它,我怎么能使用它。
答案 0 :(得分:1)
Service本身就是一个上下文。因此,如果您只需要Context
,则可以在this
课程中致电Service
。
或者,您应该在启动之前将Activity
传递给服务。确保在致电Activity
super.onCreate(bundle);
但是,您不应该从Activity
操纵Service
或它的观点。更好的方法是从您的Activity
通知 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
看起来像这样,您会收到来自Service
didReceivedNotification()
的消息
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
中的邮件发送给正在收听的所有Activities
:
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateActivity, optionalData);
哪个非常好,你不必传递Activity实例。
NotificationCenter来源来自Telegram。
答案 1 :(得分:0)
public class myService extends Service
{
public static myActivity activity;
public static void setActivity(myActivity activity)
{
this.activity = activity;
}
public void useActivityExample()
{
myService.myActivity.update();
}
}
public class myActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
// ... some things are being executed and myService is being bound
mService.setActivity(getActivity());
}
}