Android Service 服务的基本用法
来源:互联网 发布:网站数据采集器 编辑:程序博客网 时间:2024/06/05 05:14
Service 服务的基本用法
创建&绑定服务
活动与服务进行通信
创建&绑定服务
活动与服务进行通信
创建四个按钮:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.scott.servicetest.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/start_service" android:text="Start Service" android:textAllCaps="false"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/stop_service" android:text="Stop Service" android:textAllCaps="false"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bind_service" android:text="Bind Service" android:textAllCaps="false"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/unbind_service" android:text="Unbind Service" android:textAllCaps="false"/></LinearLayout>
package com.example.scott.servicetest;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.IBinder;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button startService; private Button stopService; private Button bindService; private Button unbindService; private Intent intent;//不能这么写Intent intent = new Intent(MainActivity.this,MyService.class); private MyService.DownloadBinder downloadBinder;//创建一个SerciceConnection的匿名类并重写下面的两个方法,这两个方法分别会在活动与服务成功绑定和解除绑定的时候调用 private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { //向下转型获取DownloadBinder实例 downloadBinder = (MyService.DownloadBinder) service; downloadBinder.startDownload(); downloadBinder.getProgress(); } @Override public void onServiceDisconnected(ComponentName name) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //~~~~~ intent = new Intent(MainActivity.this,MyService.class); startService = (Button) findViewById(R.id.start_service); stopService = (Button) findViewById(R.id.stop_service); bindService = (Button) findViewById(R.id.bind_service); unbindService = (Button) findViewById(R.id.unbind_service); startService.setOnClickListener(this); stopService.setOnClickListener(this); bindService.setOnClickListener(this); unbindService.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.start_service: startService(intent); break; case R.id.stop_service: stopService(intent); break; case R.id.bind_service: //这里传入的BIND_AUTO_CREATE表示在活动和服务进行绑定的时候会自动执行onCreate(),不会执行onStartCommand() bindService(intent,connection,BIND_AUTO_CREATE); break; case R.id.unbind_service: unbindService(connection); break; default: break; } }}
总结一下,onCreate()方法是在服务第一次创建的时候调用,onStartCommand()方法是在每次启动服务的时候调用。
活动与服务进行通信:在MyService中创建一个继承自Binder的实例;然后在onBind()中返回这个实例。然后再活动中绑定服务,就可以服务里的Binder提供的方法了。
关注微信公众号,每天都有优质技术文章,搞笑GIF图片推送哦。
0 0
- Android Service 服务的基本用法
- android---(Service服务的基本用法)
- Service服务的基本用法
- 服务Service的基本用法
- android学习十八(Service服务的基本用法)
- Android之Service(二):服务的基本用法
- service的基本用法+远程服务
- Android Service的基本用法
- Android Service(服务)详解·(二)Service基本用法
- 文章标题 android服务Service 的一些基本简介和用法
- Android Service基本用法
- Android下的Service的基本用法
- Android Service的基本用法(1)
- Service服务的用法
- Android service(一)基本服务
- android服务基本用法
- Service的基本用法
- Service的基本用法
- 压缩感知重构算法之基追踪降噪(Basis Pursuit De-Noising, BPDN)
- Android最佳性能实践(二)——分析内存的使用情况
- Android最佳性能实践(三)——高性能编码优化
- HDU1241 Oil Deposits
- 聊天界面
- Android Service 服务的基本用法
- 三十天完成一个Android项目(Android应用程序的构成)
- Android最佳性能实践(四)——布局优化技巧
- DNS域名轮循业务监控
- CodeBlocks快捷键大全(附汉化教程及其文件)
- POJ 2752 Seek the Name, Seek the Fame kmp算法
- HDU1253 胜利大逃亡(BFS)
- MyBatis增删改查
- BZOJ 1087: [SCOI2005]互不侵犯King