继承Binder类绑定服务显示时间
来源:互联网 发布:复旦cpu卡算法 编辑:程序博客网 时间:2024/05/17 14:16
1、布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/current_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="当前时间" android:textColor="@android:color/black" android:textSize="25dp" /></LinearLayout>
2、创建CurrentTimeService类,继承Service类,内部类LocalBinder继承Binder类
public class CurrentTimeService extends Service {private final IBinder binder = new LocalBinder();public class LocalBinder extends Binder{CurrentTimeService getService(){return CurrentTimeService.this;//返回当前服务的实例}}@Overridepublic IBinder onBind(Intent arg0) {return binder;}public String getCruurentTime(){Time time = new Time();//创建Time对象time.setToNow();//设置时间为当前时间String currentTime = time.format("%Y-%m-%d %H:%M:%S");//设置时间格式return currentTime;}}
3、MainActivity
public class MainActivity extends Activity {CurrentTimeService cts;boolean bound; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onStart() { super.onStart(); Button button = (Button)findViewById(R.id.current_time); button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this, CurrentTimeService.class);bindService(intent,sc,BIND_AUTO_CREATE);//绑定服务if(bound){//如果绑定则显示当前时间Toast.makeText(MainActivity.this, cts.getCruurentTime(), Toast.LENGTH_SHORT).show();}}}); } @Override protected void onStop() { super.onStop(); if (bound) {bound = false;unbindService(sc);//解绑定} } private ServiceConnection sc = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {bound =false;}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {LocalBinder binder = (LocalBinder)service;//获得自定义的LocalBinder对象cts = binder.getService();//获得CurrentTimeService对象bound = true;}};}
4、修改AndroidManifest.xml
<service android:name=".CurrentTimeService"></service>
0 0
- 继承Binder类绑定服务显示时间
- Android 绑定类型服务---继承Binder类
- Android 绑定类型服务---继承Binder类
- 使用Messenger类绑定服务显示时间
- android Bound Service使用:继续Binder类绑定服务
- Android Service学习之 Service生命周期 & 绑定类型服务& Binder类
- Android Service详解(四)---绑定服务BoundService详解之扩展Binder类
- 5.2 绑定服务的三种方式:扩展 Binder 类、使用 Messenger、使用 AIDL
- repeater 绑定显示时间的不同格式
- 时间类--多重继承
- Android Service 之三(Bind Service, 继承自 Binder 类)
- Android Service 之三(Bind Service, 继承自 Binder 类)
- Android Service 之(Bind Service, 继承自 Binder 类)
- Android Service 之三(Bind Service, 继承自 Binder 类)
- Android Service 之三(Bind Service, 继承自 Binder 类)
- Android Service 之三(Bind Service, 继承自 Binder 类)
- Android Service 之三(Bind Service, 继承自 Binder 类)
- Android Service 之三(Bind Service, 继承自 Binder 类)
- 续续 Codeforces 596 C Wilbur and Points
- Linux虚拟机启动级别和网络设置
- Xcode和模拟器的清除缓存
- 7、单例设计模式
- 分享周鸿祎的《如何建立一个“铁打的营盘”》
- 继承Binder类绑定服务显示时间
- boost库中 不同版本载入lib的方式
- javaEE之-------Spring中的aspectJ的应用
- 考研后的Java温习之I/O
- Android笔记--对Content的理解和使用和Application的用途、生命周期
- WMware11下安装Fedora22详细图文
- The Django Book 2.0--中文版--第四章 模板
- 23 判断扑克牌的顺子
- 位运算