[androiod]_[Service基本使用]
来源:互联网 发布:中国云计算方案大会 编辑:程序博客网 时间:2024/05/29 19:59
Service是Android中四大组件之一,在Android开发中起到非常重要的作用,先来看一下官方对Service的定义:
Service(服务)是一个没有用户界面的在后台运行执行耗时操作的应用组件。其他应用组件能够启动Service,并且当用户切换到另外的应用场景,Service将持续在后台运行。另外,一个组件能够绑定到一个service与之交互(IPC机制),例如,一个service可能会处理网络操作,播放音乐,操作文件I/O或者与内容提供者(content provider)交互,所有这些活动都是在后台进行。
Service有两种状态,“启动的”和“绑定”
通过startService()启动的服务处于“启动的”状态,一旦启动,service就在后台运行,即使启动它的应用组件已经被销毁了。通常started状态的service执行单任务并且不返回任何结果给启动者。比如当下载或上传一个文件,当这项操作完成时,service应该停止它本身。
还有一种“绑定”状态的service,通过调用bindService()来启动,一个绑定的service提供一个允许组件与service交互的接口,可以发送请求、获取返回结果,还可以通过夸进程通信来交互(IPC)。绑定的service只有当应用组件绑定后才能运行,多个组件可以绑定一个service,当调用unbind()方法时,这个service就会被销毁了。
service与activity一样都存在与当前进程的主线程中,所以,一些阻塞UI的操作,比如耗时操作不能放在service里进行,比如另外开启一个线程来处理诸如网络请求的耗时操作。如果在service里进行一些耗CPU和耗时操作,可能会引发ANR警告,这时应用会弹出是强制关闭还是等待的对话框。所以,对service的理解就是和activity平级的,只不过是看不见的,在后台运行的一个组件,这也是为什么和activity同被说为Android的基本组件。
Service生命周期中的一些方法:
代码例子:
1.首先创建Service类:
public class ServiceDemo extends Service { private static final String TAG = "ServiceDemo"; public static final String ACTION = "com.example.yepen.myapplication.ServiceDemo"; @Nullable @Override public IBinder onBind(Intent intent) { Log.i(TAG, "ServiceDemo onBind"); return null; } @Override public void onCreate() { Log.i(TAG, "ServiceDemo onCreate"); super.onCreate(); } @Override public void onStart(Intent intent, int startId) { Log.i(TAG, "ServiceDemo onStart"); super.onStart(intent, startId); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "ServiceDemo onStartCommand"); return super.onStartCommand(intent, flags, startId); }}
2.在AndroidManifest.xml中声明组件:
<service android:name=".ServiceDemo"> <intent-filter> <action android:name=".ServiceDemo"/> </intent-filter> </service>
3.在需要service的地方通过Context.startService(Intent)方法启动service或者Context.bindService方法来绑定service:
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private Button startServiceButton; private Button bindServiceButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startServiceButton = (Button) findViewById(R.id.start_server); bindServiceButton = (Button) findViewById(R.id.bind_server); startServiceButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ServiceDemo.ACTION); startService(intent); } }); bindServiceButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { bindService(new Intent(ServiceDemo.ACTION), conn, BIND_AUTO_CREATE); } }); } ServiceConnection conn = new ServiceConnection() { public void onServiceConnected(ComponentName name, IBinder service) { Log.i(TAG, "onServiceConnected"); } public void onServiceDisconnected(ComponentName name) { Log.i(TAG, "onServiceDisconnected"); } }; @Override protected void onDestroy() { Log.i(TAG, "onDestroy unbindService"); unbindService(conn); super.onDestroy(); }}
0 0
- [androiod]_[Service基本使用]
- [androiod]_[MVP框架]
- Service的基本使用
- Service的基本使用
- Android基础知识_使用Service
- GreenDao基本使用_
- Data Service - 1. 基本使用
- Service Worker的基本使用
- [Objective-c]_[NSUserDefaults]_[基本使用]
- [Cocoa]_[NSTableView]_[基本使用]
- [Cocoa]_[NSNotificationCenter]_[基本使用]
- Android:Service(一)——Service的基本使用
- Android Service 的介绍与基本使用
- Android:Service的基本使用1
- Day13-Android中Service的基本使用
- [Objective-c]_[NSString的基本使用]
- [android]_[ListView的基本使用]
- [android]_[ViewGroup的基本使用]
- 备份压缩命令 - tar
- Linux下递归遍历指定目录下的文件内容并删除的函数实现
- LeetCode-11.Container With Most Water
- java环境变量设置
- 数学
- [androiod]_[Service基本使用]
- 琐事MARK
- 5种单例模式的介绍与比较
- Java注解(Annotation)介绍
- mysql5.5配置半同步复制
- Jquery弹出框效果 自己下载看看是否搭配风格
- leetcode_c++:Maximum Product Subarray(152)
- Java 具体优势
- 关于FragmentTransaction addToBackStack无效解决方法