我是 android 和 java 的新手,遇到了一个问题。我有一个方法 Activity ,我想在后台使用它们。对于背景,我使用了服务类。问题是我不能将 Activity 对象传递给服务构造函数,因为它不能包含任何参数。我阅读了如何通过 intend 发送字符串和 double ,但我找不到如何通过 Activity 类(class)。
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 步。定义您的服务将用于传达事件的接口(interface):
public interface ServiceCallbacks {
void Showtoast(String msg);
}
第 2 步。 编写您的服务类。您的 Activity 将绑定(bind)到此服务。此外,我们将添加一个方法来设置 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 接口(interface)。当您绑定(bind)/取消绑定(bind)服务时,您将通过调用服务上的 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();
}
}
谢谢,如果有任何问题,请告诉我。
关于java - 将类对象传递给服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29649913/