《安卓系统Android应用程序开发PPT教材_第7章后台服务(12).ppt》由会员分享,可在线阅读,更多相关《安卓系统Android应用程序开发PPT教材_第7章后台服务(12).ppt(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、7.2 本地服务n7.2.3 服务绑定14.public class SimpleMathServiceDemo extends Activity 15.private MathService mathService;16.private boolean isBound=false;17.TextView labelView;18.Override19.public void onCreate(Bundle savedInstanceState)20.super.onCreate(savedInstanceState);21.setContentView(R.layout.main);22.2
2、3.labelView=(TextView)findViewById(R.id.label);24.Button bindButton=(Button)findViewById(R.id.bind);25.Button unbindButton=(Button)findViewById(R.id.unbind);26.Button computButton=(Button)findViewById(Rpute);27.28.bindButton.setOnClickListener(new View.OnClickListener()29.Override30.public void onCl
3、ick(View v)31.if(!isBound)7.2 本地服务n7.2.3 服务绑定32.final Intent serviceIntent=new Intent(SimpleMathServiceDemo.this,MathService.class);33.bindService(serviceIntent,mConnection,Context.BIND_AUTO_CREATE);34.isBound=true;35.36.);39.unbindButton.setOnClickListener(new View.OnClickListener()40.Override41.pu
4、blic void onClick(View v)42.if(isBound)43.isBound=false;44.unbindService(mConnection);45.mathService=null;46.47.48.);7.2 本地服务n7.2.3 服务绑定putButton.setOnClickListener(new View.OnClickListener()51.Override52.public void onClick(View v)53.if(mathService=null)54.labelView.setText(未绑定服务);55.return;56.57.l
5、ong a=Math.round(Math.random()*100);58.long b=Math.round(Math.random()*100);59.long result=mathService.Add(a,b);60.String msg=String.valueOf(a)+String.valueOf(b)+61.=+String.valueOf(result);62.labelView.setText(msg);63.64.);65.66.7.2 本地服务n7.2.3 服务绑定67.private ServiceConnection mConnection=new Servic
6、eConnection()68.Override69.public void onServiceConnected(ComponentName name,IBinder service)70.mathService=(MathService.LocalBinder)service).getService();71.72.73.Override74.public void onServiceDisconnected(ComponentName name)75.mathService=null;76.77.;78.http:/www.anzhuo.cc/forum-58-1.html 安卓HTC One X论坛