Java - 接口 - 如何在多个活动中一起分配特定接口
Java - Interface - How do I assign a specific interface in multiple activities all together
我有一个 service
有一个 interface
,我是 implementing
多个 activities
中的 interface callback
,但因为我在每个 activity's
onCreate
上调用应用程序 instance
,interfaces
仅在当前 activity
上响应。我如何确保它们在每个 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
}
}
每个 activity 的 onCreate
当我这样做时 MyApp.getInstance().setCallBackListener(this);
回调的焦点转移到新的 activity。但是我想同时专注于两项或多项活动,我该怎么做?我想要的解决方案有没有更好的方法?
请注意这些:
- 我不想调用那些函数
onResume
- 我不想使用
Broadcasts
我刚刚为每个 activity 创建了不同的接口并将它们注册到它们对应的 activity,它们成功了!
MyApp.getInstance().setCallBackListener(this);
我有一个 service
有一个 interface
,我是 implementing
多个 activities
中的 interface callback
,但因为我在每个 activity's
onCreate
上调用应用程序 instance
,interfaces
仅在当前 activity
上响应。我如何确保它们在每个 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
}
}
每个 activity 的 onCreate
当我这样做时 MyApp.getInstance().setCallBackListener(this);
回调的焦点转移到新的 activity。但是我想同时专注于两项或多项活动,我该怎么做?我想要的解决方案有没有更好的方法?
请注意这些:
- 我不想调用那些函数
onResume
- 我不想使用
Broadcasts
我刚刚为每个 activity 创建了不同的接口并将它们注册到它们对应的 activity,它们成功了!
MyApp.getInstance().setCallBackListener(this);