Java-接口-如何在多个活动中共同分配特定接口 [英] Java - Interface - How do I assign a specific interface in multiple activities all together

查看:90
本文介绍了Java-接口-如何在多个活动中共同分配特定接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务,其中有一个界面,我正在执行 多个活动中的接口回调,但是因为我正在调用应用程序实例在每个活动的 onCreate 上,接口仅响应当前的活动。我如何确保它们在每个活动中都协同工作。

I have a service which has an interface, I'm implementing the interface callback in multiple activities, but because of I'm calling the app instance on every activity's onCreate, the interfaces are responding on the current activity only. How do I make sure they work all together in every activity.

MyApp.java

public class MyApp extends Application {

    private static MyApp myApp;

    @Override
    public void onCreate() {
        super.onCreate();
        myApp = new MyApp();
    }

    @Contract(pure = true)
    public static synchronized MyApp getInstance() {
        MyApp myApp;
        synchronized (MyApp.class) {
            myApp = MyApp.myApp;
        }
        return myApp;
    }

    public void setCallBackListener(MyService.ReceiversCallbacks receiversCallbacks) {
        MyService.receiversCallbacks = receiversCallbacks;
    }
}

MyService.java

public class MyService extends Service {

    public static MyService.ReceiversCallbacks receiversCallbacks;

    public MyService() {
        super();
    }

    public interface ReceiversCallbacks {
        void onReceiveCallbacks(String data);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_ID)
                .setContentTitle("Background service")
                .setSmallIcon(R.drawable.ic_launcher)
                .build();
        startForeground(1, notification);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (MY_LOGIC) receiversCallbacks.onReceiveCallbacks("DATA_FROM_MY_LOGIC");

        //stopSelf();
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

ActivityA.java

public class ActivityA extends AppCompatActivity implements MyService.ReceiversCallbacks {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         MyApp.getInstance().setCallBackListener(this);
    }

    @Override
    public void onReceiveCallbacks(String data) {
           // calling important functions
    }
}

ActivityB.java

public class ActivityB extends AppCompatActivity implements MyService.ReceiversCallbacks {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         MyApp.getInstance().setCallBackListener(this);
    }

    @Override
    public void onReceiveCallbacks(String data) {
           // calling important functions
    }
}

我执行此操作时,每个活动的 onCreate > MyApp.getInstance()。setCallBackListener(this); 回调的重点转移到新活动上。但是我想同时关注两个或多个活动,该怎么做?我想要的解决方案还有什么更好的方法吗?

Each activity's onCreate when I do this MyApp.getInstance().setCallBackListener(this); The focus of the call back shifts to the new activity. But I want the focus in both or more activities at the same time, how do I do that? Is there any better way for the solution I want?

请注意以下几点:


  • 我不想调用那些函数 onResume

  • 我不想使用广播

  • I don't want to call those functions onResume
  • I don't want to use Broadcasts

推荐答案

我刚刚创建了每个活动使用不同的接口并将它们注册到相应的活动中,并且它们起作用了!

I just created different interfaces for each activity and registered them to their corresponding activity, and they worked!

MyApp.getInstance()。setCallBackListener(this);

这篇关于Java-接口-如何在多个活动中共同分配特定接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆