Service的基本使用
来源:互联网 发布:spss数据分析 编辑:程序博客网 时间:2024/06/05 09:38
复习四大组件中的Service
导语:
刚面试了一家android岗位,心里有点慌呀,不过把之前重要的基础知识捡捡应该应付日常开发没什么问题吧。哈哈,要努力学习了。
Service的定义
负责消耗长时间、持续时间的工作任务,且不提供显示界面。
Service与Activity运行在同一个线程中,如果直接在Service里运行耗时操作,则会ANR(通常是因为Activity响应时间超过5秒,或者BroadcastReceiver的响应时间超过5秒)
开发Service
a)需要在AndroidManifest.xml中注册
b ) 应该使用Intent激活
Service的生命周期
a)onCreate->onStartCommand->onDestory
启动与停止Service的实际场景之一:在Activity的onCreate或者某个响应事件中使用startService()激活Service,并在Activity的onDestory方法中使用stopService()停止Service,或者在Service中执行完任务以后调用stopService中执行完任务以后调用stopSelf()以关闭自身Service。
b )onStartCommand返回值详解,
START_STICKY:(默认值)粘性的
START_NOT_STICKEY:非粘性的,当Service被意外/强行关闭以后,不会自己重新启动
START_REDRLIVER_INTENT:粘性的重新传递Intent,当Service被意外/强行关闭以后,自动重新启动,且重新得到intent参数。
START_STICKEY_COMPATIBILITY:兼容模式的粘性
代码实现
共两个类,一个是MainActivity.java,一个是MyService.java
package com.example.myapplication;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity { private Button start_service; private Button stop_service; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setView(); setListiner(); } private void setListiner() { start_service.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent =new Intent(); intent.setClass(MainActivity.this,MyService.class); startService(intent); } }); stop_service.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { Intent intent =new Intent(); intent.setClass(MainActivity.this,MyService.class); stopService(intent); } }); } private void setView() { start_service=(Button)findViewById(R.id.btn_startService); stop_service=(Button)findViewById(R.id.btn_stopService); }}
package com.example.myapplication;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;import static android.content.ContentValues.TAG;public class MyService extends Service { public MyService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } @Override public void onCreate() { Log.e(TAG, "onCreate: "); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG, "onStartCommand: "); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.e(TAG, "onDestroy: " ); super.onDestroy(); }}
- Service的基本使用
- Service的基本使用
- Service Worker的基本使用
- Android Service 的介绍与基本使用
- Android:Service的基本使用1
- Day13-Android中Service的基本使用
- Android:Service(一)——Service的基本使用
- Service的基本组成
- Service的基本用法
- Service的基本用法
- Service的基本用法
- service的基本运用
- android中Service的基本用法和IntentService的使用
- Android中Service的基本理解和使用
- Android 四大组件之Service的基本使用
- Data Service - 1. 基本使用
- [androiod]_[Service基本使用]
- Service的基本的用法
- 73:Unique Binary Search Trees
- JavaWeb学习之SpringMVC:MAVEN搭建第一个WEB项目
- usb 设备描述符举例说明
- 前端性能优化方法--个人总结
- 排序算法分析
- Service的基本使用
- 找到信息,是一种能力,远离百度竞价排名,介绍7个更好用的搜索工具
- qscoj:喵哈哈村的狼人杀大战(5)
- 蓝桥杯: 基础练习 数列排序
- 74:Unique Binary Search Trees II
- 插入运算符“<<”(输入)的重载
- Python Restful API 资料整理
- 简单的asp.net模拟邮箱系统基础实现(一 总体功能版块的设计,与简单数据库的设计)
- get提交方式和post提交方式的区别