我只是想从我的活动中调用本地服务的方法。我怎样才能做到这一点 ?如何从活动中调用服务的方法?
回答
做到这一点的一种方法是定义一个接口Android's AIDL并利用Binder
子系统来执行IPC。在我发布的链接上有一组很棒的说明。我会从那里开始,然后在这里发帖,如果你有问题。尽管是一个非常复杂的主题(IPC)Android和Binder
做了一个非常好的工作,使其非常简单(至少要开始,我敢肯定,如果你想,可以使它变得复杂;-))
编辑正如在评论中指出的那样,如果Service
和客户端在相同的进程中运行,这是不必要的。除非您另行指定,否则这是默认设置。但是,它仍然可以工作,它只会增加一点复杂性。
我不知道你的问题在哪里,请张贴一些代码。 使用活页夹,活动可以访问服务对象。请参阅API中的示例以创建活动和服务之间的连接。
在您的活动中拥有服务对象,您可以简单地调用:
mService.yourMethod();
如果您确切地描述您的问题,并且正如我所说,发布一些片段,我们可以帮助您更好。
你为什么不把一些链接到有用的资源?发布一些关于如何绑定服务并随后调用其方法的框架片段? :) – Juri 2011-01-30 20:44:45
@Juri http://stackoverflow.com/questions/1916253/bind-service-to-activity-in-android无处它被链接 – CrandellWS 2017-01-13 12:42:09
有这个权利的服务文档中的示例代码,在“本地服务样本”:
http://developer.android.com/reference/android/app/Service.html#LocalServiceSample
也为那些人暗示AIDL - 如果你的服务和客户端是所有部分你拥有.apk并在同一进程中运行(默认行为),则不需要aidl;这只是额外的复杂性,不会给你任何东西。
下面是一个例子,这可能有助于
Server.java:
package com.example.bindservice.binder;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class Server extends Service {
IBinder mBinder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public class LocalBinder extends Binder {
public Server getServerInstance() {
return Server.this;
}
}
public String getTime() {
SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return mDateFormat.format(new Date());
}
}
Client.java
package com.example.bindservice.binder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.bindservice.binder.Server.LocalBinder;
public class Client extends Activity {
boolean mBounded;
Server mServer;
TextView text;
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView)findViewById(R.id.text);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
text.setText(mServer.getTime());
}
});
}
@Override
protected void onStart() {
super.onStart();
Intent mIntent = new Intent(this, Server.class);
bindService(mIntent, mConnection, BIND_AUTO_CREATE);
};
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(Client.this, "Service is disconnected", 1000).show();
mBounded = false;
mServer = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Toast.makeText(Client.this, "Service is connected", 1000).show();
mBounded = true;
LocalBinder mLocalBinder = (LocalBinder)service;
mServer = mLocalBinder.getServerInstance();
}
};
@Override
protected void onStop() {
super.onStop();
if(mBounded) {
unbindService(mConnection);
mBounded = false;
}
};
}
- 1. 如何从服务中调用活动中的方法
- 2. 如何从服务中调用活动方法
- 3. 服务如何与其活动通信?服务如何调用启动该服务的活动中的方法?
- 4. 如何从其调用活动访问服务的方法?
- 5. 如何从调用活动类中调用服务类的stopservice()方法
- 6. 调用方法从服务中更新活动中的TextViews
- 7. 如何从活动调用服务?
- 8. 如何从BroadcastReceiver调用活动方法?
- 9. 从公共服务方法调用活动时的NullPointerException
- 10. Android:调用从服务到活动的方法
- 11. 调用一个活动的方法,从服务
- 12. 如何从服务中调用主要活动中的功能?
- 13. 来自服务的运行活动中的调用方法
- 14. 从服务调用的活动
- 15. 在Android中:如何从服务调用活动的功能?
- 16. 如何从服务中启动活动?
- 17. Android - 如何从活动中调用片段中的方法
- 18. 如何从活动中调用片段中的方法
- 19. 如何确保服务从特定活动中被调用
- 20. 从非活动类别的不同活动中调用方法
- 21. 如何从Android中的活动调用片段方法?
- 22. 服务如何调用活动?官方示例不起作用!
- 23. 如何从Azure移动服务自定义Api调用方法?
- 24. 如何从活动中停止服务
- 25. 从导出的活动调用方法
- 26. 如何隐藏从服务调用的活动android
- 27. 如何从.NET 4.0中的JS调用服务器方法
- 28. 如何从服务启动活动?
- 29. 调用方法,当活动解除绑定服务
- 30. 从外部类调用活动方法
你的链接不工作... – 2013-02-12 09:33:34