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

时间:2015-08-15 13:36:36

标签: java android

我正在开发一个应用程序,它从网络获取数据并将其写回网络。为此,我使用的是AsyncTask,但这些请求未经过优化,因此我必须在每次请求时对用户进行身份验证。现在我想使用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;
            }
        }
        }
    }
}

这是我用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) {

            }

        };

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

0 个答案:

没有答案