我是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;
}
}
&#13;
感谢您的帮助!
答案 0 :(得分:2)
您好请参阅以下步骤确定帮助您。
第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();
}
}
如果您有任何疑问,请与我们联系。
答案 1 :(得分:0)
为什么这么复杂?您可以轻松地从服务中展示烤面包 因此,您可以将代码置于服务的onCreate方法
中Toast.makeText(this, "text", Toast.LENGTH_SHORT).show();