Android Service简介(系列1)
2015-10-24 01:16
471 查看
Android Service简介(系列1)
Android Service是Android重要的组件,在开发中会有用到和涉及。本文先给出一个最简单的Android Service例子。
(第1步)写一个类继承自Service,假设这个类的名字叫做MyAppService.java,重点是完成两个方法:
Service的onCreate和onStartCommand方法。
onCreate仅仅在Service第一次被startService时候初始化操作一次,随后不管再怎么startService,都不会再onCreate了。
耗时的、后台的、不需要用户交互的操作放在onStartCommand里面处理。需要强调一点,Android的Service并不是一个单独的进程、线程空间,是和Android主线程共享进程空间,这就意味,不要在onStartCommand方法里面阻塞主线程,否则将造成ANR!如果在onStartCommand里面有耗时操作,那么务必将onStartCommand里面的耗时操作代码块放到线程里面做。(注意!次说仅仅针对Service,IntentService和Service机制不同,不存在此问题,但有其自身特点,后面文章再说。)
Service的onDestroy只会被调用一次,那就是Service被stopService或者stopSelf时候。
完整的MyAppService.java代码:
package zhangphil.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyAppService extends Service {
// 仅仅create一次
@Override
public void onCreate() {
Log.d(this.getClass().getName(), "onCreate");
}
// 在这里处理耗时、后台、无需用户交互的‘重口味’操作。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(this.getClass().getName(), "hello,service!");
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
Log.d(this.getClass().getName(), "onDestroy");
}
}
(第2步)在AndroidManifest.xml定义第1步写的Service,在本例中是MyAppService,如代码:
<application
...
<service android:name="zhangphil.service.MyAppService" />
...
</application>
(第3步)接下来就可以在自己的activity中通过startService和stopService启动、停止Service了,如代码所示:
Intent intent=new Intent(activity,MyAppService.class);
activity.startService(intent);
...
Intent intent=new Intent(activity,MyAppService.class);
activity.stopService(intent);
Android Service简介(系列1)
Android Service是Android重要的组件,在开发中会有用到和涉及。本文先给出一个最简单的Android Service例子。
(第1步)写一个类继承自Service,假设这个类的名字叫做MyAppService.java,重点是完成两个方法:
Service的onCreate和onStartCommand方法。
onCreate仅仅在Service第一次被startService时候初始化操作一次,随后不管再怎么startService,都不会再onCreate了。
耗时的、后台的、不需要用户交互的操作放在onStartCommand里面处理。需要强调一点,Android的Service并不是一个单独的进程、线程空间,是和Android主线程共享进程空间,这就意味,不要在onStartCommand方法里面阻塞主线程,否则将造成ANR!如果在onStartCommand里面有耗时操作,那么务必将onStartCommand里面的耗时操作代码块放到线程里面做。(注意!次说仅仅针对Service,IntentService和Service机制不同,不存在此问题,但有其自身特点,后面文章再说。)
Service的onDestroy只会被调用一次,那就是Service被stopService或者stopSelf时候。
完整的MyAppService.java代码:
package zhangphil.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyAppService extends Service {
// 仅仅create一次
@Override
public void onCreate() {
Log.d(this.getClass().getName(), "onCreate");
}
// 在这里处理耗时、后台、无需用户交互的‘重口味’操作。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(this.getClass().getName(), "hello,service!");
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
Log.d(this.getClass().getName(), "onDestroy");
}
}
(第2步)在AndroidManifest.xml定义第1步写的Service,在本例中是MyAppService,如代码:
<application
...
<service android:name="zhangphil.service.MyAppService" />
...
</application>
(第3步)接下来就可以在自己的activity中通过startService和stopService启动、停止Service了,如代码所示:
Intent intent=new Intent(activity,MyAppService.class);
activity.startService(intent);
...
Intent intent=new Intent(activity,MyAppService.class);
activity.stopService(intent);
相关文章推荐
- Android第三方框架之xListView的使用方法
- 基于Android的CPU+GPU的异构编程开发,目前主要有以下几种平台
- Android视频播放(一)
- NDK编译Android字符界面的可执行程序
- android导入项目时 R文件错误
- Android自定制Toast显示外观
- Android Native Crash Capture SDK
- android 最优绘制圆图方法
- Android-获取系统的应用程序的信息
- 怎样搭高质量的Android项目框架,框架的结构具体描述?
- setOnPageChangeListener方法提示过期
- android framework层简单研究
- android中添加级联更新的下拉菜单
- android带参和不带参的页面跳转形式
- Android-多击事件处理
- Android【基础】 Gradle的初步使用
- Android--.9.png图片绘制说明
- Android的“美味”发展历程
- Android 中的 Service 全面总结
- Android开发中解决Toast长时间显示问题