2015-08-15 147 views
1

我正在開發一個應用程序,它從Web獲取數據並將其寫回到Web。爲此,我使用AsyncTask,但這些請求沒有進行優化,所以我必須根據每個請求對用戶進行身份驗證。現在我想使用Service來保持連接到後端,並執行請求,而無需在每個請求上對用戶進行身份驗證。如何將服務對象傳遞給Android中的子類?

我無法將此服務對象發送到子活動,而無需創建新實例並將我需要的所有值從服務中清零。那麼如何做到這一點沒有價值?

這裏是我的服務:

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的?

回答

3

好了,所以你需要一個服務對象,劃痕正從基地活動對象的引用,而創建一個單例類,它是整個應用程序全局訪問.. 的源代碼,以幫助您低於

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



    } 


} 
+0

謝謝,那解決了我的服務對象問題,但是現在我有networkOnMainThread問題。希望解決這個問題。 – Ollikas

+0

歡迎@Ollikas,我很高興我可以幫助 – KudzieChase

+0

解決這些問題,但我無法通過認證,所以每個請求大約需要5秒鐘才能完成。我認爲它是因爲我用來與後端進行通信的庫 – Ollikas