Binder_service加法
来源:互联网 发布:淘宝刷单兼职hao360 编辑:程序博客网 时间:2024/05/17 06:29
package com.example.binder_service;import com.example.binder_service.MyAppService.MyBinder;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.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {private ServiceConnection sc;private MyAppService myAppService;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);sc=new ServiceConnection(){@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {MyBinder mBinder = (MyBinder)service;myAppService = mBinder.getService();}@Overridepublic void onServiceDisconnected(ComponentName name) {}};Button start = (Button) findViewById(R.id.start);start.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {startService();}});Button bind=(Button) findViewById(R.id.bind);bind.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {bindService();}});Button getValue=(Button) findViewById(R.id.get);getValue.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {int result = myAppService.getServiceIntValue();Log.d("服务获取的值", result+"");}});}private void startService() {Intent intent = new Intent(this,MyAppService.class);startService(intent);}private void bindService() {Intent mIntent = new Intent(this,MyAppService.class);bindService(mIntent,sc,Service.BIND_AUTO_CREATE);}@Overrideprotected void onDestroy() {super.onDestroy();Intent intent = new Intent(this, MyAppService.class);stopService(intent);}}
package com.example.binder_service;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class MyAppService extends Service{private String str;privateint a=1,b=3,result=-1;@Overridepublic void onCreate() {Log.d(this.getClass().getName(), "onCreate");}//需要Service处理的操作在此:@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d("onStartCommand", result+"");this.result=a+b;return super.onStartCommand(intent, flags, startId);}public int getServiceIntValue() {return result;}public void setServiceValue(int a,int b) {this.a=a;this.b=b;}@Overridepublic IBinder onBind(Intent intent) {return new MyBinder();}public class MyBinder extends Binder{public MyAppService getService(){return MyAppService.this;}}@Overridepublic boolean onUnbind(Intent intent) {return super.onUnbind(intent);}@Overridepublic void onDestroy() {}}
0 0
- Binder_service加法
- 加法
- 加法
- 不用加法做加法。。
- 高精度加法
- 二进制加法
- 高精度加法
- 多项式加法
- PopMenu加法
- 大数加法
- d002: 加法
- 多项式加法
- 大数加法
- 加法进位
- 大数加法
- 大数加法
- 十六进制加法
- 加法多线程
- 第八周 数据结构实践项目——串 【项目2 - 建立链串的算法库】
- 详解spring事务属性
- .NET异步编程:IO完成端口与BeginRead
- asp.net试题解析
- 第十周项目--【输出所有的叶子节点】
- Binder_service加法
- Java基础程序设计
- 【Unity】通过AnimatorController更改其中某个动画片断的速度
- log4j以不同的级别往不同文件(屏幕)打印日志的方法
- 404页面配置
- MAC OS 下使用Android Studio获取开发版和发布版SHA1证书
- 正则表达式摘要
- 第八周 数据结构之自建算法库——链串
- SimpleDateFormat与ThreadLocal