android Bound Service使用:继续Binder类绑定服务
来源:互联网 发布:淘宝交易量查询 编辑:程序博客网 时间:2024/04/29 20:27
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/current_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="当前时间" android:textColor="@android:color/white" android:textSize="25dp"/></RelativeLayout>
CurrentTimeService.java:
package com.example.demoboundservice;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.text.format.Time;public class CurrentTimeService extends Service{private final IBinder binder = new LocalBinder();public class LocalBinder extends Binder{CurrentTimeService getService(){return CurrentTimeService.this;}}public IBinder onBind(Intent intent){return binder;}public String getCurrentTime(){Time time = new Time();time.setToNow();String currentTime = time.format("%Y-%m-%d %H:%M:%S");return currentTime;}}
MainActivity.java:
package com.example.demoboundservice;import com.example.demoboundservice.CurrentTimeService.LocalBinder;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}CurrentTimeService cts ;boolean bound;protected void onStart(){super.onStart();Button button = (Button)findViewById(R.id.current_time);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent = new Intent(MainActivity.this,CurrentTimeService.class);bindService(intent,sc,BIND_AUTO_CREATE);if(bound){Toast.makeText(MainActivity.this, cts.getCurrentTime(), Toast.LENGTH_LONG).show();}}});}protected void onStop(){super.onStop();if(bound){bound = false;unbindService(sc);}}private ServiceConnection sc = new ServiceConnection(){public void onServiceDisconnected(ComponentName name){bound = false;}public void onServiceConnected(ComponentName name,IBinder service){LocalBinder binder =(LocalBinder)service;cts = binder.getService();bound = true;}};}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demoboundservice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.demoboundservice.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.example.demoboundservice.CurrentTimeService"/> </application></manifest>
0 0
- android Bound Service使用:继续Binder类绑定服务
- android Bound Service使用:使用Message类绑定服务
- Android Bound Service(一) ----- Extending Binder Service(进程内绑定Service的简单例子)
- Android 03 Bound Service--之被绑定的服务
- Android 开发指南(二) 服务绑定 Bound Service
- Android bound service 详解二:派生Binder类
- Android bound service 详解二:派生Binder类
- Android bound service 详解二:派生Binder类
- 【安卓篇】Android bound service 详解二:派生Binder类
- Android Service学习之 Service生命周期 & 绑定类型服务& Binder类
- android - Bound Services 绑定服务
- 绑定服务——Bound Service
- Android Service详解(四)---绑定服务BoundService详解之扩展Binder类
- Android 绑定类型服务---继承Binder类
- Android 绑定类型服务---继承Binder类
- Bound Service 之 Binder
- Bound服务之Binder
- android - 绑定服务(bound services)
- NPOI2.1在docx页脚插入页码
- 【四圣龙神录的编程教室】第20章、我们来绘制背景吧
- S5PV210开发板 介绍camera 驱动 (4)
- yum [Errno 256] No more mirrors to try 解决方法
- 【Leetcode】Min Stack
- android Bound Service使用:继续Binder类绑定服务
- ios 中根据给定日期 获取星期几
- CRUD进阶
- U盘安装CENTOS6.5+WIN7双系统(win7下安装centos6.5)
- 编程珠玑11章——快速排序的改进
- linux学习之 V4L2的摄像头应用
- Java内存泄露原因详解
- 对一个简单数学问题的浮想
- 水的效果