2011-01-30 72 views

回答

1

做到这一点的一种方法是定义一个接口Android's AIDL并利用Binder子系统来执行IPC。在我发布的链接上有一组很棒的说明。我会从那里开始,然后在这里发帖,如果你有问题。尽管是一个非常复杂的主题(IPC)Android和Binder做了一个非常好的工作,使其非常简单(至少要开始,我敢肯定,如果你想,可以使它变得复杂;-))

编辑正如在评论中指出的那样,如果Service和客户端在相同的进程中运行,这是不必要的。除非您另行指定,否则这是默认设置。但是,它仍然可以工作,它只会增加一点复杂性。

+0

你的链接不工作... – 2013-02-12 09:33:34

0

我不知道你的问题在哪里,请张贴一些代码。 使用活页夹,活动可以访问服务对象。请参阅API中的示例以创建活动和服务之间的连接。

在您的活动中拥有服务对象,您可以简单地调用:
mService.yourMethod();
如果您确切地描述您的问题,并且正如我所说,发布一些片段,我们可以帮助您更好。

+4

你为什么不把一些链接到有用的资源?发布一些关于如何绑定服务并随后调用其方法的框架片段? :) – Juri 2011-01-30 20:44:45

+0

@Juri http://stackoverflow.com/questions/1916253/bind-service-to-activity-in-android无处它被链接 – CrandellWS 2017-01-13 12:42:09

35

下面是一个例子,这可能有助于
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; 
     } 
    }; 
} 
相关问题