简体   繁体   English

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

[英]How to pass service object to subclass in Android?

I'm developing an app, which gets data from web and writes it back to web. 我正在开发一个应用程序,该应用程序从Web获取数据并将其写回到Web。 For that I was using AsyncTask, but those requests weren't optimized, so I had to authenticate user, on every request. 为此,我使用了AsyncTask,但是这些请求并未进行优化,因此我必须对每个请求进行用户身份验证。 Now I want to use Service for holding the connection to backend and to do requests without needing to authenticate the user on every requests. 现在,我想使用Service来保持与后端的连接并执行请求,而无需在每个请求上都对用户进行身份验证。

I'm having trouble to send this service object to subactivity without creating an new instance and nulling all the values I need from the service. 我无法将此服务对象发送给子活动,而又没有创建新实例并使该服务所需的所有值都为零的麻烦。 So how to do it without osing values ? 那么如何在不损失价值的情况下做到这一点呢?

Here is my service: 这是我的服务:

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;
            }
        }
        }
    }
}

And here is where I bind it with BaseActivity: 这是我与BaseActivity绑定的地方:

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) {

            }

        };

My question is how to get the same instance of the service in some of the subactivities of BaseActivity? 我的问题是如何在BaseActivity的某些子活动中获取相同的服务实例?

Okay so you need a service object, Scratch getting the object's reference from base activity rather create a singleton class that is globally accessible throughout your application.. The source code to help you is below 好的,因此您需要一个服务对象,即从基础活动中获取该对象的引用,而不是创建一个在整个应用程序中可全局访问的单例类。帮助您的源代码如下

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*/;



    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM