2015-04-15 191 views
0

我是android和java中的新成員,遇到一個問題。我有一個活動的方法,我想在後臺使用它們。爲了後臺,我使用了一個Service類。問題是我不能將活動對象傳遞給服務構造函數,因爲它不能包含任何參數。我讀了如何通過打算髮送字符串和雙打,但我找不到如何傳遞活動類。將類對象傳遞給服務

public class MainActivity extends Activity 
 
{ 
 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState){ 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_main); 
 
    } 
 

 
    @Override 
 
    public void onStop(){ 
 
     super.onStop(); 
 
     startService(new Intent(this, MyService.class)); 
 
    } 
 

 
    @Override 
 
    public void onResume(){ 
 
     super.onResume(); 
 
     stopService(new Intent(this, MyService.class)); 
 
    } 
 

 
    public void toast(String msg) { 
 
     Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); 
 
    } 
 
} 
 

 
//--------------------------------------- 
 

 
public class MyService extends Service 
 
{ 
 
    private MainActivity main; 
 

 
    public MyService() { 
 
    } 
 

 
    @Override 
 
    public void onCreate() { 
 
     super.onCreate(); 
 
     main.toast("test"); // <- here I want to use some method 
 
    } 
 

 
    @Override 
 
    public IBinder onBind(Intent intent) { 
 
     return null; 
 
    } 
 
}

感謝您的幫助!

回答

1

您好,請參閱下面的步驟肯定幫助你。

第1步:定義您的服務將用於通信事件的接口:

public interface ServiceCallbacks { 
    void Showtoast(String msg); 
} 

第2步:寫您的服務類。您的活動將綁定到此服務。另外,我們將添加一個方法來設置ServiceCallbacks。

public class MyService extends Service { 
    // Binder given to clients 
    private final IBinder binder = new LocalBinder(); 
    // Registered callbacks 
    private ServiceCallbacks serviceCallbacks; 


    // Class used for the client Binder. 
    public class LocalBinder extends Binder { 
     MyService getService() { 
      // Return this instance of MyService so clients can call public methods 
      return MyService.this; 
     } 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return binder; 
    } 

    public void setCallbacks(ServiceCallbacks callbacks) { 
     serviceCallbacks = callbacks; 
    } 
} 

第3步:寫遵循同樣的指導你的Activity類,也使它實現您ServiceCallbacks接口。當您從服務綁定/解除綁定時,您將通過調用服務上的setCallbacks來註冊/取消註冊。

public class MyActivity extends Activity implements ServiceCallbacks { 
    private MyService service; 
    private boolean bound = false; 

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

    @Override 
    protected void onStart() { 
     super.onStart(); 
     // bind to Service 
     Intent intent = new Intent(this, MyService.class); 
     bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     // Unbind from service 
     if (bound) { 
      service.setCallbacks(null); // unregister 
      unbindService(serviceConnection); 
      bound = false; 
     } 
    } 

    /** Callbacks for service binding, passed to bindService() */ 
    private ServiceConnection serviceConnection = new ServiceConnection() { 

     @Override 
     public void onServiceConnected(ComponentName className, 
       IBinder service) { 
      // cast the IBinder and get MyService instance 
      LocalBinder binder = (LocalBinder) service; 
      service = binder.getService(); 
      bound = true; 
      service.setCallbacks(this); // register 
     } 

     @Override 
     public void onServiceDisconnected(ComponentName arg0) { 
      bound = false; 
     } 
    }; 

    /* Defined by ServiceCallbacks interface */ 
    @Override 
    public void Showtoast(String message) { 
     Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); 
    } 
} 

謝謝你讓我知道如果你有任何問題。

+0

謝謝,只是一個錯誤在此字符串「service.setCallbacks(本); //註冊」,因爲爲MyService和本地同名的IBinder – cy8g3n

+0

你能如果這個答案正確,請投票回答! –

0

爲什麼這麼複雜?您可以輕鬆地從服務中顯示敬酒。
所以,你可以把你的代碼到服務的onCreate方法

Toast.makeText(this, "text", Toast.LENGTH_SHORT).show(); 
+0

這裏只舉例,想法是從服務中調用活動方法 – cy8g3n

+0

實際上你需要什麼?爲什麼這個邏輯應該完全在Activity內部? – cooperok