这个例子来自“安卓巴士”,经阅读,理解,写此文章,希望通过这篇一个Demo学完Android中所有的服务对对广大读者有所帮助。
说明:这个例子实现了Android中常见的许多服务,下面是实现的截图
接下来,以源代码的方式分析这个例子
1.MainActivity--主界面
这个类主要是实现用户所看到的这个Activity,其中包含了一系列的按钮,用户点击按钮执行相应的动作,所以在这个类中主要是对按钮的定义和对按钮绑定相应的监听器,下面是实现的代码:
- packagelovefang.stadyService;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.widget.Button;
- importandroid.view.View;
- importandroid.content.Intent;
- importandroid.util.Log;
- publicclassMainStadyServicsextendsActivity{
- ButtonstartServiceButton;
- ButtonshutDownServiceButton;
- ButtonstartBindServiceButton;
- ButtonsendBroadcast;
- ButtonnotificationButton;
- ButtonalarmButton;
- ButtonhandlerButton;
- ButtonasyncButton;
- ButtonphoneStateButton;
- ButtoncallphoneButton;
- ButtonvibratorButton;
- CountServicecountService;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- Log.v("MainStadyServics","setContentView");
- setContentView(R.layout.main);
- getWidget();
- regiestListener();
- }
- publicvoidgetWidget(){
- startServiceButton=(Button)findViewById(R.id.startServerButton);
- startBindServiceButton=(Button)findViewById(R.id.startBindServerButton);
- shutDownServiceButton=(Button)findViewById(R.id.sutdownServerButton);
- sendBroadcast=(Button)findViewById(R.id.sendBroadcast);
- notificationButton=(Button)findViewById(R.id.notification);
- alarmButton=(Button)findViewById(R.id.alarm);
- handlerButton=(Button)findViewById(R.id.handler);
- asyncButton=(Button)findViewById(R.id.async);
- phoneStateButton=(Button)findViewById(R.id.phonestate);
- callphoneButton=(Button)findViewById(R.id.callphone);
- vibratorButton=(Button)findViewById(R.id.vibrator);
- }
- publicvoidregiestListener(){
- startServiceButton.setOnClickListener(startService);
- shutDownServiceButton.setOnClickListener(shutdownService);
- startBindServiceButton.setOnClickListener(startBinderService);
- sendBroadcast.setOnClickListener(broadcastReceiver);
- notificationButton.setOnClickListener(notification);
- alarmButton.setOnClickListener(startAlarm);
- handlerButton.setOnClickListener(handler);
- asyncButton.setOnClickListener(async);
- phoneStateButton.setOnClickListener(phonestate);
- callphoneButton.setOnClickListener(callphoneEvent);
- vibratorButton.setOnClickListener(vibrator);
- }
- publicButton.OnClickListenerstartService=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,CountService.class);
- startService(intent);
- Log.v("MainStadyServics","startService");
- }
- };
- publicButton.OnClickListenershutdownService=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,CountService.class);
- stopService(intent);
- Log.v("MainStadyServics","shutDownserveice");
- }
- };
- publicButton.OnClickListenerstartBinderService=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UseBrider.class);
- startActivity(intent);
- Log.v("MainStadyServics","startBinderService");
- }
- };
- publicButton.OnClickListenerbroadcastReceiver=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UseBroadcast.class);
- startActivity(intent);
- Log.v("MainStadyServics","startbroadcast");
- }
- };
- publicButton.OnClickListenernotification=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UseNotification.class);
- startActivity(intent);
- Log.v("MainStadyService","startNotification");
- }
- };
- publicButton.OnClickListenerstartAlarm=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UseAlarmManager.class);
- startActivity(intent);
- Log.v("MainStadyService","startalarm");
- }
- };
- publicButton.OnClickListenerhandler=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UseHandleMessage.class);
- startActivity(intent);
- Log.v("MainStadyService","starthandle");
- }
- };
- publicButton.OnClickListenerasync=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UseAsyncTask.class);
- startActivity(intent);
- Log.v("MainStadyService","starthandle");
- }
- };
- publicButton.OnClickListenerphonestate=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UsePhoneState.class);
- startActivity(intent);
- Log.v("MainStadyService","startphonestate");
- }
- };
- publicButton.OnClickListenercallphoneEvent=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UseActionCall.class);
- startActivity(intent);
- Log.v("MainStadyService","startcallphone");
- }
- };
- publicButton.OnClickListenervibrator=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UseVibrator.class);
- startActivity(intent);
- Log.v("MainStadyService","startcallphone");
- }
- };
- protectedvoidonDestroy(){
- super.onDestroy();
- Intentintent=newIntent(MainStadyServics.this,CountService.class);
- stopService(intent);
- }
- }
2.启动服务按钮
这个类实现的是第一个按钮的功能,在这个类中新开了一个线程,并每隔一秒打印出一行日志
代码如下:
- packagelovefang.stadyService;
- importandroid.app.Service;
- importandroid.os.IBinder;
- importandroid.os.Binder;
- importandroid.content.Intent;
- importandroid.util.Log;
- publicclassCountServiceextendsService{
- booleanthreadDisable;
- intcount;
- publicIBinderonBind(Intentintent){
- returnnull;
- }
- publicvoidonCreate(){
- super.onCreate();
- newThread(newRunnable(){
- publicvoidrun(){
- while(!threadDisable){
- try{
- Thread.sleep(1000);
- }catch(InterruptedExceptione){
- }
- count++;
- Log.v("CountService","Countis"+count);
- }
- }
- }).start();
- }
- publicvoidonDestroy(){
- super.onDestroy();
- this.threadDisable=true;
- }
- publicintgetConunt(){
- returncount;
- }
- classServiceBinderextendsBinder{
- publicCountServicegetService(){
- returnCountService.this;
- }
- }
- }
3.绑定服务
服务有两种实现的方法:
(1)startService,启动服务,这时需要程序员管理服务的生命周期
(2)bindService,绑定服务,此时Service与Activity绑定在一起
下面是实现的代码:
- packagelovefang.stadyService;
- importandroid.app.Activity;
- importandroid.content.ComponentName;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.content.ServiceConnection;
- importandroid.os.Bundle;
- importandroid.os.IBinder;
- importandroid.util.Log;
- publicclassUseBriderextendsActivity{
- CountServicecountService;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(newUseBriderFace(this));
- Intentintent=newIntent(UseBrider.this,CountService.class);
- bindService(intent,conn,Context.BIND_AUTO_CREATE);
- }
- privateServiceConnectionconn=newServiceConnection(){
- publicvoidonServiceConnected(ComponentNamename,IBinderservice){
- countService=((CountService.ServiceBinder)service).getService();
- }
- publicvoidonServiceDisconnected(ComponentNamename){
- countService=null;
- }
- };
- protectedvoidonDestroy(){
- super.onDestroy();
- this.unbindService(conn);
- Log.v("MainStadyServics","out");
- }
- }
接下来为您介绍发送广播、Notification通知、Alarm 闹钟等服务的具体内容
分享到:
相关推荐
Service 是一个可以在后台执行长时间运行操作而不提供用户界面的应用组件。服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行。 此外,组件可以绑定到服务,以与之进行交互,甚至是...
再开始之前我们还是先介绍下service吧:此处用的是IntentService,至于和常规的service有什么区别呢? 有了Service为什么还要有个IntentService呢? 原因如下: 1)Service默认运行在主线程中,IntentService运行在一个...
loud Service V2.2培训材料 Cloud service v2.2版本说明docx Cloud service V2.2教学进度表docX Cloud service V2.2课程表xsX Cloud service V2.2培训材料 Cloud service V2.2实验环境搭建指南.docX Cloud service ...
Wiley.SOA MoSOA Modeling Patterns for Service Oriented Discovery and Analysis Wiley | 2010-01-07 | ISBN: 0470481978 | 480 pages | PDF | 7 MB SOA Modeling Patterns for Service-Oriented Discovery and ...
版本: Version: ... 如果以前安装了 Visual Studio 2008 修补程序或 Visual Studio 2008 SP1 预发行版本,则必须在安装 Visual Studio 2008 SP1 之前运行 Service Pack 准备工具。 如果安装了多个
ServiceMesh(服务网格)概念在社区里头非常火,有人提出2018年是ServiceMesh年,还有人提出ServiceMesh是下一代的微服务架构基础。作为架构师,如果你现在还不了解ServiceMesh的话,是否感觉有点落伍了?那么到底什么...
service now web service guide
C# 开机启动服务(windows service) 本程序用ASP.NET C#开发,不针对系统版本限制。 本源码拿就来可以直接写您想要的服务,无需再改其它的代码,拿来主义是不是很好?例如订单服务。 本服务是100%源码,源码中包含: ...
oracle service_name参数
JAVA程序部署为Windows Service,包含:JavaService-2.0.10(32位).zip与JavaService_2_0_10-amd64(64位).zip
可利用javaService将java程序安装为系统服务
目录 HCIP-Cloud Service Solutions Architect V3.0 HCIP-Cloud Service Solutions Architect V3.0 版本说明 HCIP-Cloud Service Solutions Architect V3.0 实验手册
只需要将service文件替换你的arcgis安装目录下的arcgis10.1\License10.1\bin下的service.txt,将计算机名字改为自己的名字即可。
SharePoint 2013 introduces a new distributed cache service based on Windows Server AppFabric distributed caching. The distributed cache is used in features like authentication, to cache FedAuth ...
Service Worker 是 Chrome 团队提出和力推的一个 WEB API,用于给 web 应用提供高级的可持续的后台处理能力。 service worker 能够操作的缓存是有别于浏览器内部的 memory cache 或者 disk cache。它是独立于当前...
tomcat service.bat配置tomcat成服务。
用于安装arcgis 10.0时破解。将service.txt复制到服务下的bin文件夹,将原有的service.txt覆盖
微博Service mesh大规模实践微博
android,启动service,并且service把值传给Activity显示
install Windows Service 需要大于20个字符,不支持HTML标签。