当前位置:K88软件开发文章中心编程语言APP编程Android01 → 文章内容

4.2.3 Service精通

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-12 6:26:15

合,这里在静态代码块中进行初始化,当然你可也以在构造方法中完成初始化 static { ss.put(new Person(1, "Jay"), new Salary("码农", 2000)); ss.put(new Person(2, "GEM"), new Salary("歌手", 20000)); ss.put(new Person(3, "XM"), new Salary("学生", 20)); ss.put(new Person(4, "MrWang"), new Salary("老师", 2000)); } @Override public void onCreate() { super.onCreate(); salaryBinder = new SalaryBinder(); } @Override public IBinder onBind(Intent intent) { return salaryBinder; } //同样是继承Stub,即同时实现ISalary接口和IBinder接口 public class SalaryBinder extends Stub { @Override public Salary getMsg(Person owner) throws RemoteException { return ss.get(owner); } } @Override public void onDestroy() { System.out.println("服务结束!"); super.onDestroy(); } } 注册下Service:<service android:name=".AidlService"> <intent-filter> <action android:name="android.intent.action.AIDLService" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service>2——客户端编写Step 1:把服务端的AIDL文件拷贝下,拷贝后目录如下:Step 2:编写简单的布局,再接着就是核心MainActvitiy的实现了定义一个ServciceConnection对象,重写对应方法,和前面的普通数据的类似再接着在bindService,然后再Button的点击事件中获取Salary对象并显示出来!MainActivity.javapackage com.jay.example.aidl_complexclient; import com.jay.example.aidl.ISalary; import com.jay.example.aidl.Person; import com.jay.example.aidl.Salary; import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private ISalary salaryService; private Button btnquery; private EditText editname; private TextView textshow; private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { salaryService = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { //返回的是代理对象,要调用这个方法哦! salaryService = ISalary.Stub.asInterface(service); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnquery = (Button) findViewById(R.id.btnquery); editname = (EditText) findViewById(R.id.editname); textshow = (TextView) findViewById(R.id.textshow); Intent it = new Intent(); it.setAction("com.jay.aidl.AIDL_SERVICE"); bindService(it, conn, Service.BIND_AUTO_CREATE); btnquery.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { String name = editname.getText().toString(); Salary salary = salaryService.getMsg(new Person(1,name)); textshow.setText(name + salary.toString()); }catch(RemoteException e){e.printStackTrace();} } }); } @Override protected void onDestroy() { super.onDestroy(); this.unbindService(conn); } } 运行截图:PS: 这里的代码是之前用Eclipse写的代码,Android Studio下自定义类型有点问题,暂时没找到解决方法,如果知道的朋友请告知下!!!万分感激!!!出现的问题如下:两个实例的代码下载(基于Eclipse的):1)使用AIDL完成进程间的简单通信2)传递复杂数据的AIDL Service的实现3.直接通过Binder的onTransact完成跨进程通信上面讲过Android可以通过Binder的onTrensact方法来完成通信,下面就来简单试下下,还是前面那个根据序号查询名字的例子:服务端实现:/** * Created by Jay on 2015/8/18 0018. */public class IPCService extends Service{ private static final String DESCRIPTOR = "IPCService"; private final String[] names = {"B神","艹神","基神","J神","翔神"}; private MyBinder mBinder = new MyBinder(); private class MyBinder extends Binder { @Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { switch (code){ case 0x001: { data.enforceInterface(DESCRIPTOR); int num = data.readInt(); reply.writeNoException(); reply.writeString(names[num]); return t

上一页  [1] [2] [3] [4] [5] [6]  下一页


4.2.3 Service精通