核心组件之service
来源:互联网 发布:miss淘宝店网址 编辑:程序博客网 时间:2024/06/06 00:04
新建类 echoservice.java 继承service
然后 AndroidManifest.xml 中添加
echoservice.java
package com.itheima.service;import java.util.Timer;import java.util.TimerTask;import android.R.integer;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class echoservice extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub System.out.println("onBind"); return echoServiceBinder; }//被final修饰的变量就是一个常量,只能赋值一次private final EchoServiceBinder echoServiceBinder = new EchoServiceBinder(); public class EchoServiceBinder extends Binder{ // EchoServiceBinder 扩展自Binder public echoservice getservice(){ return echoservice.this; }}public int getCurrentNumber(){ return i;} @Override public void onCreate() { // TODO Auto-generated method stub System.out.println("on Creat"); starttimer();//起动timer super.onCreate(); } @Override public void onDestroy() { // TODO Auto-generated method stub System.out.println("on Destroy"); stoptimer();//停止timer super.onDestroy(); } private int i = 0; public void starttimer(){ if(timer == null){ timer = new Timer(); task = new TimerTask() { @Override public void run() { i++; System.out.println(i); } }; timer.schedule(task, 1000, 1000); } } public void stoptimer(){ if(timer != null){ task.cancel(); timer.cancel(); timer = null; task = null; } } private Timer timer=null; private TimerTask task=null;}
MainActivity.java
package com.itheima.service;import java.net.ContentHandler;import android.support.v7.app.ActionBarActivity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends ActionBarActivity implements OnClickListener, ServiceConnection {private Button btnStartService,btnStopService,btnBindService,btnUnbindService;private Intent serviceIntent; private echoservice service = null; private Button btnGetCurrentNumber;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); serviceIntent = new Intent(this, echoservice.class); btnStartService = (Button) findViewById(R.id.btnstartservice); btnStopService = (Button) findViewById(R.id.btnstopservice); btnBindService = (Button) findViewById(R.id.btnBindService); btnUnbindService = (Button) findViewById(R.id.btnUnbindService); btnGetCurrentNumber = (Button) findViewById(R.id.btnGetCurrentNumber); btnStartService.setOnClickListener(this); btnStopService.setOnClickListener(this); btnBindService.setOnClickListener(this); btnUnbindService.setOnClickListener(this); btnGetCurrentNumber.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnstartservice: startService(serviceIntent);//开启service break; case R.id.btnstopservice: stopService(serviceIntent);//停止service break; case R.id.btnBindService: bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);//绑定service break; case R.id.btnUnbindService: unbindService(this); service = null; break; case R.id.btnGetCurrentNumber: System.out.println("当前数字"+service.getCurrentNumber()); break; } } @Override public void onServiceConnected(ComponentName name, IBinder binder) {//绑定成功后执行 System.out.println("onServiceConnected"); service =((echoservice.EchoServiceBinder) binder).getservice();//强制类型转换 } @Override public void onServiceDisconnected(ComponentName name) { //service崩溃时执行 // TODO Auto-generated method stub } }
activity_main.xml
<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.itheima.service.MainActivity" > <Button android:id="@+id/btnstartservice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start service" /> <Button android:id="@+id/btnstopservice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="stop service" /> <Button android:id="@+id/btnBindService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="bind service" /> <Button android:id="@+id/btnUnbindService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="unbind service" /> <Button android:id="@+id/btnGetCurrentNumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="get current number" /></LinearLayout>
阅读全文
0 0
- 核心组件之service
- Android核心组件之Service
- Android核心组件之Service
- Android四大核心组件之Service
- Android四大核心组件之Service:服务
- Android 四大核心组件之Service(1) 学习心得
- 【Android基础】(2)四大核心组件之Service
- Android核心基础-11.Android四大组件之Service
- Android四大核心组件之Service|AIDL使用详解
- 核心组件之Activity
- 深入浅出 Android核心组件Service(4)
- 深入浅出 Android核心组件Service(1)
- 深入浅出 Android核心组件Service(2)
- 深入浅出 Android核心组件Service(3)
- 深入浅出 Android核心组件Service(4)
- 深入浅出 Android核心组件Service(5)
- 安卓核心组件二service
- android组件之Service
- file,uri,path相互转化
- 主界面实现_功能菜单
- 丑人就要多读书
- 2017.8.8 B组模拟
- 有关LCS
- 核心组件之service
- Elasticsearch 乐观锁处理
- 黑匣子
- 1.面试题目汇总-嵌入式篇
- python开发部署时新增数据库中表的方法
- 堆排序
- 案例:Shell脚本检测网站url是否正常运行
- 【NYOJ-94】cigarettes
- 明明在包含目录里包含了cv.h的路径,为什么还是报错error C1083: 无法打开包括文件: “opencv/cv.h”: No such file or directory