问题描述
我看过很多职位,关于这一点,但不能得到调用,从服务类的活动方法精确,最简便的方法。广播接收器只有选择吗?没有捷径 ?我只需要调用活动类下面的方法后,媒体播放器ppared在服务类$ P $。
活动类:
公共无效的UpdateProgress(){
//设置进度条的值
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
//更新进度条
updateProgressBar();
}
服务类:
@覆盖
公众的IBinder onBind(意向意图){
Log.d(this.getClass()的getName(),装订);
返回musicBind;
}
@覆盖
公共布尔onUnbind(意向意图){
返回false;
}
@覆盖
公共无效于prepared(MediaPlayer的MP){
尝试 {
mp.start();
}赶上(IllegalStateException异常E){
e.printStackTrace();
}
//的UpdateProgress(); //这里需要调用该活动的方法
}
定义一个接口您的服务将使用通讯事件:
公共接口ServiceCallbacks {
无效DoSomething的();
}
写您的服务类。您的活动将绑定到该服务,所以按照这里显示的样本。此外,我们将添加一个方法来设置 ServiceCallbacks
。
公共类的MyService延伸服务{
//粘合剂给客户端
私人最终的IBinder粘合剂=新LocalBinder();
//注册回调
私人ServiceCallbacks serviceCallbacks;
//用于客户端粘结剂类。
公共类LocalBinder扩展粘合剂{
为MyService的getService(){
//返回的MyService这个实例,以便客户端可以调用的公共方法
返回MyService.this;
}
}
@覆盖
公众的IBinder onBind(意向意图){
返回粘合剂;
}
公共无效setCallbacks(ServiceCallbacks回调){
serviceCallbacks =回调;
}
}
编写遵循同样的指导你的活动类,但也使它实现您ServiceCallbacks接口。当您绑定/解除绑定的服务,您将注册/注销通过调用 setCallbacks
上的服务。
公共类MyActivity扩展活动实现ServiceCallbacks {
私人的MyService为myService;
私人布尔约束= FALSE;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(...);
}
@覆盖
保护无效的OnStart(){
super.onStart();
//绑定到服务
意向意图=新的意图(这一点,MyService.class);
bindService(意向,serviceConnection,Context.BIND_AUTO_CREATE);
}
@覆盖
保护无效的onStop(){
super.onStop();
//取消绑定的服务
如果(绑定){
myService.setCallbacks(空); //注销
unbindService(serviceConnection);
势必= FALSE;
}
}
/ **回调服务结合,传递给bindService()* /
私人ServiceConnection serviceConnection =新ServiceConnection(){
@覆盖
公共无效onServiceConnected(组件名的className,服务的IBinder){
//投的IBinder并获得为MyService实例
LocalBinder粘合剂=(LocalBinder)服务;
为myService = binder.getService();
势必= TRUE;
myService.setCallbacks(MyActivity.this); // 寄存器
}
@覆盖
公共无效onServiceDisconnected(组件名称为arg0){
势必= FALSE;
}
};
/ *通过ServiceCallbacks接口中定义* /
@覆盖
公共无效DoSomething的(){
...
}
}
现在,当你的服务要传达回活动,只需拨打从早期的接口方法之一。在你的服务:
如果(serviceCallbacks!= NULL){
serviceCallbacks.doSomething();
}
I have seen many posts in SO regarding this but could not get the exact and most easy way to call an activity method from service class. Is broadcast receiver only the option? No easy way out ? I just need to call the following method in Activity class after the media player is prepared in Service class .
Activity class:
public void updateProgress() {
// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
// Updating progress bar
updateProgressBar();
}
Service class:
@Override
public IBinder onBind(Intent intent) {
Log.d(this.getClass().getName(), "BIND");
return musicBind;
}
@Override
public boolean onUnbind(Intent intent) {
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
try {
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
}
// updateProgress();// Need to call the Activity method here
}
Define an interface your Service will use to communicate events:
public interface ServiceCallbacks {
void doSomething();
}
Write your Service class. Your Activity will bind to this service, so follow the sample shown here. In addition, we will add a method to set the 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;
}
}
Write your Activity class following the same guide, but also make it implement your ServiceCallbacks interface. When you bind/unbind from the Service, you will register/unregister it by calling setCallbacks
on the Service.
public class MyActivity extends Activity implements ServiceCallbacks {
private MyService myService;
private boolean bound = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
}
@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) {
myService.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;
myService = binder.getService();
bound = true;
myService.setCallbacks(MyActivity.this); // register
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
bound = false;
}
};
/* Defined by ServiceCallbacks interface */
@Override
public void doSomething() {
...
}
}
Now when your service wants to communicate back to the activity, just call one of the interface methods from earlier. Inside your service:
if (serviceCallbacks != null) {
serviceCallbacks.doSomething();
}
这篇关于从服务类中调用活动类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!