[Android Develop_004] Android Background Service
来源:互联网 发布:管家婆软件哪里买 编辑:程序博客网 时间:2024/06/08 12:38
Android的很多功能都使用到了后台服务,就像Windows应用程序大量依赖后台服务一样。服务的作用无需赘言,Android应用程序是如何实现后台服务的呢。今天写了一个创建和停止后台服务的简单例子,在此提供给大家做参考。当然Android后台服务的实际应用肯定比这个复杂,更为复杂的情况我们今后再做探讨。
ServiceActivity
<span style="font-family:Microsoft YaHei;">package com.example.androidexample;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class ServiceActivity extends Activity {private Button btnStartService;private Button btnStopService;private Intent intent;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);super.setContentView(R.layout.activity_service);intent = new Intent();intent.setAction("My_Action");btnStartService = (Button)super.findViewById(R.id.btnStartService);btnStopService = (Button)super.findViewById(R.id.btnStopService);btnStartService.setOnClickListener(startServiceListener);btnStopService.setOnClickListener(stopServiceListener);}OnClickListener startServiceListener = new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubstartService(intent);}};OnClickListener stopServiceListener = new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubstopService(intent);}};}</span>从这个Activity里我们注意到Activity和Service的关联是通过Intent实现的。通过设置Intent的Action来指定动作的执行者。这里还需要说的一点是关于Service的Manifest.xml配置文件。这里面需要指定Action和Service的类名。
<span style="font-family:Microsoft YaHei;"><application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.androidexample.ServiceActivity" 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.business.FirstService"> <intent-filter> <action android:name="My_Action"></action> </intent-filter> </service> </application></span>
这里我们能够更加清楚地看到Activity是如何跟Service连接起来的。
我们使用的布局很简单,就是两个按钮。
<span style="font-family:Microsoft YaHei;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btnStartService" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/StartService"/> <Button android:id="@+id/btnStopService" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/StopService"/></LinearLayout></span>
这里还有一个重要的角色类不可忽视,那就是Service类。
<span style="font-family:Microsoft YaHei;">package com.example.business;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.widget.Toast;import com.example.androidexample.*;public class FirstService extends Service {@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();System.out.println("Service is created.");//Toast.makeText(new ServiceActivity(), "Service is created", Toast.LENGTH_LONG).show();}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();System.out.println("Service is destoryed.");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {System.out.println("Service is started.");// TODO Auto-generated method stubreturn super.onStartCommand(intent, flags, startId);}@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubreturn null;}}</span>这个类很好理解,是做Service的启动和停止的。注意这里的System.out.println()不是往console里面输出,而是输出在LogCat的消息里面。
点击StartService 和 StopService后我再LogCat上能看到如下消息:
下一个章节我们会讨论
0 0
- [Android Develop_004] Android Background Service
- Background Service in Android Using IntentService
- Create a background service in Android
- android background
- android:background
- android TextView background属性
- Android增加Activity background
- android src background 区别
- android:background="#eeeedd"
- android view background 问题
- android ImageView src background
- android ImageView src background
- Android background背景图片平铺
- Android 背景图片重复 Background repeat
- Android change background color smoothly
- cocos2d-x android background transprent
- android seekbar progress background失效
- Android row column onSelect background
- 安卓学习之路-隐式跳转注意事项
- play junit 单元测试
- 输出随机数1到511*511序列
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第6章节--在SharePoint2013中开发、集成和构建应用程序 总结
- 递归算法理解
- [Android Develop_004] Android Background Service
- 解决Eclipse空格自动补全问题
- gdb学习
- java volatile
- jdk 安装
- windows下运行的linux服务器批量管理工具(带UI界面)
- ubuntu下安装mysql出现FATAL ERROR: Could not find mysqld的处理
- merge用法(读书笔记)
- Mockups知识点