2015-04-15 36 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