activity数据接口负责启动该服务包。service获取数据。手术。
详细demo如下面:
package com.example.android_service_trance; import android.annotation.SuppressLint; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.Parcel; import android.os.RemoteException; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import com.example.android_service_trance.MyService.LocalBinder; @SuppressLint("Recycle") public class MainActivity extends Activity { private Button bindService=null; private Button callService=null; private Button communicationService=null; private TextView tv=null; private boolean flag=false;//默觉得不绑定 private MyService myService=null; private LocalBinder binder;//服务里的对象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindService=(Button)this.findViewById(R.id.button1); callService=(Button)this.findViewById(R.id.button2); communicationService=(Button)this.findViewById(R.id.button3); tv=(TextView)this.findViewById(R.id.textView1); bindService.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //获得意图 Intent intent=new Intent(MainActivity.this,MyService.class); //绑定服务 bindService(intent, connection, Context.BIND_AUTO_CREATE); } }); callService.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(flag){ int result=myService.getRandom(); tv.setText("<<<<<<<<"+result); } } }); communicationService.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //往service中传递值的对象 Parcel data=Parcel.obtain(); data.writeInt(10086); data.writeString("李华"); Parcel reply=Parcel.obtain(); try { binder.transact(IBinder.LAST_CALL_TRANSACTION, data, reply, 0); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } //从service里读数据 Log.i("Main<<<<<<<<",reply.readString()); Log.i("Main<<<<<<<<<",reply.readInt()+""); } }); } //解除绑定 @Override protected void onStop() { super.onStop(); if(flag) { //解除绑定 unbindService(connection); flag=false; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } //连接activity和service的桥梁 private ServiceConnection connection=new ServiceConnection(){ @Override public void onServiceConnected(ComponentName arg0, IBinder iBinder) { //连接 //获得LocalBinder binder=(LocalBinder)iBinder; //获得MyService实例 myService=binder.getService(); flag=true; } @Override public void onServiceDisconnected(ComponentName arg0) { //不连接 flag=false; }}; }
service端:
/** *Version: *author:YangQuanqing *Data: */ package com.example.android_service_trance; import java.util.Random; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.os.Parcel; import android.os.RemoteException; import android.util.Log; /** * @author YangQuanqing yqq * */ public class MyService extends Service { //定义一个随机数用于測试 private Random random=new Random(); private LocalBinder lb=new LocalBinder(); //获得当前类的实例 public class LocalBinder extends Binder{ public MyService getService() { return MyService.this; } @Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { //Activity里获取数据 Log.i("SERVICE<<<<<<<<",data.readString()); Log.i("SERVICE<<<<<<<<<",data.readInt()+""); reply.writeString("小名"); reply.writeInt(1990); return super.onTransact(code, data, reply, flags); } } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return lb; } //获得一个随机数 public int getRandom() { return random.nextInt(22); } }
版权声明:本文博客原创文章,博客,未经同意,不得转载。
时间: 2024-10-07 23:02:14