Service的基本的用法
来源:互联网 发布:nginx只允许访问index 编辑:程序博客网 时间:2024/06/04 19:06
(1.)这边是Activity对Service操作的类
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
*
* @项目名:ServiceTest_demo1
* @类名称:MainActivity
* @类描述: 服务基本用法
* @创建人:HXF
* @修改人:
* @创建时间:2015-8-13 上午11:22:54
* @version
*
*/
public class MainActivity extends Activity implements OnClickListener {
private Button startService;private Button stopService;private Button bindService;private Button unbindService;/** 拿到服务的对象 */private MyService.DownLoadBinder downLoadBinder;/** 匿名类,里面的方法在成功绑定的时候和解除绑定的时候调用 */private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { downLoadBinder = (MyService.DownLoadBinder)service; downLoadBinder.startDownload(); downLoadBinder.getProgress(); } @Override public void onServiceDisconnected(ComponentName name) { }};@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init();}/** 初始化控件 */private void init() { startService = (Button) findViewById(R.id.start_sercice); stopService = (Button) findViewById(R.id.stop_sercice); bindService = (Button) findViewById(R.id.bind_sercice); unbindService = (Button) findViewById(R.id.UnBind_sercice); startService.setOnClickListener(this); stopService.setOnClickListener(this); bindService.setOnClickListener(this); unbindService.setOnClickListener(this);}@Overridepublic void onClick(View v) { switch (v.getId()) { case R.id.start_sercice: /** 开启服务 */ Intent startIntent = new Intent(this,MyService.class); startService(startIntent); break; case R.id.stop_sercice: /** 停止服务 */ Intent stopIntent = new Intent(this,MyService.class); stopService(stopIntent); break; case R.id.bind_sercice: /** 绑定服务 */ Intent bindIntent = new Intent(this,MyService.class); bindService(bindIntent, connection, BIND_AUTO_CREATE); break; case R.id.UnBind_sercice: /** 解开绑定服务 */ unbindService(connection); break; default: break; }}
}
(2.)继承Service解释一些简单的方法的使用
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
/* 服务的基本用法和方法的解释 /
public class MyService extends Service{
private DownLoadBinder mBinder = new DownLoadBinder();@Overridepublic IBinder onBind(Intent intent) { return mBinder;}/** 会在服务创建的时候调用 */@Overridepublic void onCreate() { super.onCreate(); Log.d("MyService", "onCreate executed");}/** 会在每次服务启动的时候调用 */@Overridepublic int onStartCommand(Intent intent, int flags, int startId) { Log.d("MyService", "onStartCommand executed"); return super.onStartCommand(intent, flags, startId);}/** 会在每次服务销毁的时候调用 */@Overridepublic void onDestroy() { super.onDestroy(); Log.d("MyService", "onDestroy executed");}/** * * @项目名:ServiceTest_demo1 * @类名称:DownLoadBinder * @类描述: 绑定服务与activity设置 * @创建人:HXF * @修改人: * @创建时间:2015-8-13 上午11:52:46 * @version * */class DownLoadBinder extends Binder{ /** 开始下载 */ public void startDownload(){ System.out.println("开始下载---startDownload"); } /** 查看下载进度 */ public int getProgress(){ System.out.println("查看下载进度---getProgress"); return 0; }}
}
- Service的基本用法
- Service的基本用法
- Service的基本用法
- Service的基本的用法
- Android Service的基本用法
- Service服务的基本用法
- 服务Service的基本用法
- Android下的Service的基本用法
- Android Service 服务的基本用法
- service的基本用法+远程服务
- android---(Service服务的基本用法)
- Android Service的基本用法(1)
- 整理 Anglarjs的service,directve的基本用法
- android中Service的基本用法和IntentService的使用
- android学习十八(Service服务的基本用法)
- Android之Service(二):服务的基本用法
- Service的基本组成
- Service的基本使用
- 登录时的记住密码隐藏密码 及MD5加密
- poj 3630 Phone List(Trie)
- jsp文件ajax不执行
- ActiveX中无标题CDHtmlDialog客户区的拖动实现
- 廖雪峰的Python教程
- Service的基本的用法
- Docker学习笔记(3)-- 如何使用Dockerfile构建镜像
- libgdx ModelInstance 的transform成员
- 归并排序--c语言实现
- Java 8十个lambda表达式案例
- JS学习十四天----服务器端执行JS代码
- Android 定制EditText 改变 底线颜色
- MYGUI和HGE
- I学霸官方免费教程四十二 :Java流之字节流 输入流和输出流 InputStream和OutputStream