Service的使用
2015-10-20 17:35
483 查看
首先说一下Service(服务)是什么,服务是Android的四大组件,运行在后台的没有界面的Activity,后台是不依赖UI就可以独立存在的,服务一般在内存不够用的情况下才会被杀死,不然只能手动关闭,如果你发现你的应用很耗电,看看后台Service多不多,关掉一些没有用的Service.
给大家贴上简单代码,明天给大家上传一个多线程上传断电续传的demo
package com.xiaojie.com.xiaojie.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
/**
* 创建Service服务
* Created by Administrator on 2015/10/20.
*/
public class DownloadService extends Service {
public static final String tag = "DownloadService";
public MyBinder binder = new MyBinder();
@Override
public void onCreate() {
super.onCreate();
Log.e(tag, "onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(tag, "onStartCommand()");
new Thread(){
@Override
public void run() {
//开始后台执行任务
}
}.start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(tag, "onDestroy()");
}
public class MyBinder extends Binder {
public void startDownLoad() {
Log.e(tag, "downLoad()");
new Thread(){
@Override
public void run() {
//开始后台执行任务
}
}.start();
}
}
}
大家可以运行看下效果。多点点
大家可以发现,Service在第一次启动时会先执行onCreate,然后执行onStartCommand方法,之后添加再次调用只会执行onStartCommand方法。
启动服务分为两种方式,一种是startServer,一种是bindServer.
当一个Service被startServer后,只能被StopServer,服务被startServer后,并且还可以被bind,
bindServer方便与Activity进行交互,把Server的binder对象控制权交给Activity.
如果是startServer只能在onstartCommand方法中通过Handler然后发送广播在Activity中接收
Server分为两种,一种是本地服务,和远程服务
给大家贴上简单代码,明天给大家上传一个多线程上传断电续传的demo
package com.xiaojie.com.xiaojie.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
/**
* 创建Service服务
* Created by Administrator on 2015/10/20.
*/
public class DownloadService extends Service {
public static final String tag = "DownloadService";
public MyBinder binder = new MyBinder();
@Override
public void onCreate() {
super.onCreate();
Log.e(tag, "onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(tag, "onStartCommand()");
new Thread(){
@Override
public void run() {
//开始后台执行任务
}
}.start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(tag, "onDestroy()");
}
public class MyBinder extends Binder {
public void startDownLoad() {
Log.e(tag, "downLoad()");
new Thread(){
@Override
public void run() {
//开始后台执行任务
}
}.start();
}
}
}
package com.xiaojie.com.xiaojie.ui; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; import android.widget.Button; import com.xiaojie.com.xiaojie.R; import com.xiaojie.com.xiaojie.service.DownloadService; /** * Created by Administrator on 2015/10/20. */ public class ServiceActivity extends Activity implements View.OnClickListener { private Button bt_start_server; private Button bt_stop_server; private Button bt_bind_server; private Button bt_unbind_server; private DownloadService.MyBinder binder; ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { binder = (DownloadService.MyBinder) iBinder; binder.startDownLoad(); } @Override public void onServiceDisconnected(ComponentName componentName) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.atv_service); bt_start_server = (Button) findViewById(R.id.bt_start_server); bt_stop_server = (Button) findViewById(R.id.bt_stop_server); bt_bind_server = (Button) findViewById(R.id.bt_bind_server); bt_unbind_server = (Button) findViewById(R.id.bt_unbind_server); bt_bind_server.setOnClickListener(this); bt_stop_server.setOnClickListener(this); bt_start_server.setOnClickListener(this); bt_unbind_server.setOnClickListener(this); } @Override public void onClick(View view) { Intent intent = null; switch (view.getId()) { case R.id.bt_start_server: intent = new Intent(ServiceActivity.this, DownloadService.class); startService(intent); break; case R.id.bt_stop_server: intent = new Intent(ServiceActivity.this, DownloadService.class); stopService(intent); break; case R.id.bt_bind_server: intent = new Intent(ServiceActivity.this, DownloadService.class); bindService(intent, serviceConnection, BIND_AUTO_CREATE); break; case R.id.bt_unbind_server: unbindService(serviceConnection); break; } } }
大家可以运行看下效果。多点点
大家可以发现,Service在第一次启动时会先执行onCreate,然后执行onStartCommand方法,之后添加再次调用只会执行onStartCommand方法。
启动服务分为两种方式,一种是startServer,一种是bindServer.
当一个Service被startServer后,只能被StopServer,服务被startServer后,并且还可以被bind,
bindServer方便与Activity进行交互,把Server的binder对象控制权交给Activity.
如果是startServer只能在onstartCommand方法中通过Handler然后发送广播在Activity中接收
Server分为两种,一种是本地服务,和远程服务
相关文章推荐
- Android IPC进程间通讯机制
- android之定时器AlarmManager
- Seafile Server本地权限提升漏洞(CVE-2014-5443)
- 从USB安装Ubuntu Server 10.04.3 图文详解
- axis备忘
- 使用zabbix监控Nginx活动状态--Part1
- 高效访问Internet-启用ISA Server的缓存
- windows server域用户提升到本地更高权限组中的方法
- 完美解决SQL Server 安装问题:以前的某个程序安装已在安装计算机上创建挂起的文件操作
- IIS 错误 Server Application Error 详细解决方法
- 在WINXP系统上安装SQL Server企业版的方法
- SQL Server 存储过程的分页
- SQL Server复制需要有实际的服务器名称才能连接到服务器
- SQL Server 2000向SQL Server 2008 R2推送数据图文教程
- SQL server 表操作介绍
- SQL Server 2000 注入防护大全(二)
- SQL Server 2000 注入防护大全(一)
- 怎样把Windows server 2003转换成工作站系统
- Windows Server 2007四月CTP发布 下载
- IIS运行错误 Server Application Error 错误代码 Error: 8004的解决方法