第一行代码 第10章 服务 -- 前台服务与IntentService
来源:互联网 发布:数据库基础知识点 编辑:程序博客网 时间:2024/05/17 04:56
1、前台服务
与后台服务区别:它会一直有一个正在运行的图标在系统的状态栏显示,类似通知。
创建前台服务
在服务的onCreate()方法中添加一下代码:
Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification notification = new NotificationCompat.Builder(this) .setContentTitle("这是一个前台服务") .setContentText("前台服务显示的内容") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setContentIntent(pendingIntent) .build(); startForeground(1, notification);
通过调用startForeground()方法,将service变成一个前台服务,并在系统状态栏显示出来
startForeground()方法有两个参数
第一个参数是通知的id,标识符
第二个参数是Notification对象。
IntentService
前面我们创建的服务都是运行在主线程中,如果在服务中处理一些耗时的逻辑,就很容易出现ANR(Application Not Responding)的情况。
这个时候就需要我们在服务的每个具体的方法中开启一个子线程,然后在子线程中处理耗时逻辑。
类似如下情况:
@Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("MyService", "onStartCommand executed"); new Thread(new Runnable(){ @Override public void run(){ // 处理逻辑 // 处理完成后,调用stopSelf(),自动停止服务 stopSelf(); } } ).start(); return super.onStartCommand(intent, flags, startId); }
为了避免在创建服务时忘记开启线程和调用stopSelf()方法,Android提供了IntentService类来处理。
MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnStart = (Button)findViewById(R.id.btnStart); btnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.d("MainActivity", "MainActivity thread id is : " + Thread.currentThread().getId()); Intent startIntent = new Intent(MainActivity.this, MyIntentService.class); startService(startIntent); } }); }}
创建IntentService
public class MyIntentService extends IntentService { public MyIntentService() { super("MyIntentService"); } @Override protected void onHandleIntent(Intent intent) { Log.d("MyIntentService", "MyIntentService thread id is : " + Thread.currentThread().getId()); } @Override public void onDestroy() { Log.d("MyIntentService", "onDestroy executed"); super.onDestroy(); }}
提供的是一个无参的构造函数,并且必须在其内部调用父类的有参构造函数。
onHandleIntent()方法就在子线程中运行的,可以在这里处理耗时的逻辑。当逻辑处理完后,服务会自动停止。
阅读全文
0 0
- 第一行代码 第10章 服务 -- 前台服务与IntentService
- 第一行代码 第10章 服务 -- Handler与AsyncTask
- 第一行代码 服务
- 第一行代码 第10章 服务 -- 活动与服务进行通信
- 第一行代码 第10章 服务 -- 基本用法
- 第一行代码 第10章 服务 -- 生命周期
- [Android][第一行代码][第 10 章 服务]
- 第一行代码总结:9.3服务的基本用法——使用IntentService
- 第一行代码总结:9.3服务的基本用法——服务的更多技巧(使用前台服务)
- 《第一行代码--Android》读书笔记之多线程与服务
- 第一行代码笔记4:服务
- 《第一行代码》读书笔记(九)----服务
- 第一行代码-9.4 服务的生命周期
- 记《第一行代码》读后感---Service服务
- 《第一行代码》学习笔记之服务
- 服务(Service)-《Android第一行代码》笔记
- 学习笔记《第一行代码》——第10章 服务
- 《第一行代码》读书笔记:第十一章/位置服务
- 如何判断一个数是否是4的幂
- 【Docker】保存镜像、 容器到本地, 从本地加载镜像、 容器
- java遇见html---jsp(2)
- 【POJ
- ContenProvider数据内容提供者,增删改查
- 第一行代码 第10章 服务 -- 前台服务与IntentService
- 市场研究中的数据分析知识整理 (一)
- execve系统调用
- 《数据结构学习与实验指导》5-3:电话聊天狂人
- 杭电1879 继续畅通工程
- 输出字符串的所有排列组合
- 开门人和关门人 (sort排序)
- 集群和分布式初探
- 1444 破坏道路