gpt4 book ai didi

java - 如何将服务对象传递给 Android 中的子类?

转载 作者:行者123 更新时间:2023-11-29 05:01:18 25 4
gpt4 key购买 nike

我正在开发一个应用程序,它从网络获取数据并将其写回网络。为此,我使用了 AsyncTask,但这些请求并未优化,因此我必须在每个请求上对用户进行身份验证。现在我想使用 Service 来保持与后端的连接并执行请求,而无需在每个请求上对用户进行身份验证。

我无法在不创建新实例并清空服务所需的所有值的情况下将此服务对象发送到子 Activity 。那么如何在不设定值(value)观的情况下做到这一点呢?

这是我的服务:

public class LasteMVService extends Service {

private ArrayList<WorkSheet> workSheetArrayList;
private SpreadSheet selectedSpeadSheet;
private static String LOG_TAG = "LasteMVService";
private IBinder mBinder = new MyBinder();


@Override
public void onCreate() {
super.onCreate();
new Thread(new Task()).start();

Log.v(LOG_TAG, "in onCreate");

}


@Override
public IBinder onBind(Intent intent) {
Log.v(LOG_TAG, "in onBind");
return mBinder;
}

@Override
public void onRebind(Intent intent) {
Log.v(LOG_TAG, "in onRebind");
super.onRebind(intent);

}


public SpreadSheet getSelectedSpeadSheet() {
return selectedSpeadSheet;
}

public ArrayList<WorkSheet> getWorkSheetArrayList() {
return workSheetArrayList;
}

@Override
public void onDestroy() {
Log.v(LOG_TAG, "in onDestroy");
super.onDestroy();
}

public class MyBinder extends Binder {
public LasteMVService getService() {
return LasteMVService.this;
}
}

class Task implements Runnable {

@Override
public void run() {
synchronized (this){
AndroidAuthenticator androidAuthenticator = new AndroidAuthenticator(getApplicationContext());
SpreadSheetFactory spf = SpreadSheetFactory.getInstance(androidAuthenticator);
ArrayList<SpreadSheet> sheets = spf.getSpreadSheet("Stardiprotokoll", true);
if (sheets != null) {
final SpreadSheet spreadSheet = sheets.get(0);
selectedSpeadSheet = spreadSheet;
ArrayList<WorkSheet> workSheet = spreadSheet.getAllWorkSheets();
workSheetArrayList = workSheet;
}
}
}
}
}

这里是我将它与 BaseActivity 绑定(bind)的地方:

public class BaseActivity extends AppCompatActivity {


protected LasteMVService lasteMVService;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lasteMVService = ((LasteMVApplication) getApplicationContext()).getLasteMVService();
if(lasteMVService == null){
((LasteMVApplication) getApplicationContext()).initApp();
lasteMVService = ((LasteMVApplication) getApplicationContext()).getLasteMVService();
Util.restartApp(this, true);
}


}

@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(BaseActivity.this,LasteMVService.class);
startService(intent);
bindService(intent,serviceConnection,BIND_AUTO_CREATE);


}
protected ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
LasteMVService.MyBinder myBinder = (LasteMVService.MyBinder) service;
lasteMVService = myBinder.getService();

}

@Override
public void onServiceDisconnected(ComponentName name) {

}

};

我的问题是如何在 BaseActivity 的一些子 Activity 中获取相同的服务实例?

最佳答案

好的,所以您需要一个服务对象,Scratch 从基本 Activity 中获取对象的引用,而不是创建一个在整个应用程序中可全局访问的单例类。帮助您的源代码如下

public class Common extends Application {

private Service mService; //Service Object from your class that extends service.
private Context mContext;

public Service getService(){
return mService;
}
public void setService(Service service){
mService = service;
}



@Override
public void onCreate() {
super.onCreate();
//Gets the context associated with the whole application
mContext = getApplicationContext();
}
}


public class Service extends Service{

private Common mApp;
private Context mContext;


//Instantiate the Common Object from the singleton class within on create.
@Override
public void onCreate() {
super.onCreate();
mContext = getApplicationContext();

mApp = (Common) getApplicationContext();
mApp.setService(this);

}

}

//Now to use it in your base Activity

public class BaseActivity extends AppCompatActivity{

private Common mApp;
private Service mService;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mApp = (Common) mContext.getApplicationContext();

//To use the service you would simply use:

mService = mApp.getService()./*Some public method defined in your service*/;



}


}

关于java - 如何将服务对象传递给 Android 中的子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32025266/

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