Android开发之Service
1.创建、配置Service
package com.wwj;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class FirstService extends Service{
//必须实现的方法
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
// Service被创建时回调该方法
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("Service is Created");
}
// Service被启动时回调该方法
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
System.out.println("Service is started");
}
// Service被关闭之前回调
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("Service is Destroyed");
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wwj"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ServiceActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 配置一个Service组件 -->
<service android:name=".FirstService">
<intent-filter >
<!-- 为该Service组件的intent-filter配置action -->
<action android:name="com.wwj.FIRST_SERVICE"/>
</intent-filter>
</service>
</application>
2.启动和停止Service
package com.wwj;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ServiceActivity extends Activity {
/** Called when the activity is first created. */
private Button start,stop;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取程序界面中的start、stop两个按钮
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
//创建启动Service的Intent
final Intent intent = new Intent();
//为Intent设置Action属性
intent.setAction("com.wwj.FIRST_SERVICE");
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//启动指定Service
startService(intent);
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//停止指定Service
stopService(intent);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动Service"/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭Service"
/>
</LinearLayout>
以上为项目:Service的代码
项目运行效果如下:
DDMS的Logcat面板会显示如下信息:
3.绑定本地Service并与之通信
创建项目:BindService
代码如下:
==>BindService.java
package com.wwj;
import android.app.Service;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Binder;
import android.os.IBinder;
public class BindService extends Service{
private int count;
private boolean quit;
//定义onBinder方法所返回的对象
private MyBinder binder = new MyBinder();
//通过继承Binder来实现IBinder类
public class MyBinder extends Binder{
public int getCount(){
//获取Service的运行状态:count
return count;
}
}
//必须实现的方法
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("Service is Binded ");
//返回IBinder对象
return binder;
}
//Service被创建时回调该方法
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("Service is Created");
//启动一条线程、动态地修改count状态值
new Thread(){
public void run() {
while(!quit){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
}
count++;
}
};
}.start();
}
//Service被断开连接时回调该方法
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("Service is Unbinded");
return true;
}
//Service被关闭之前回调
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
this.quit = true;
System.out.println("Service is Destroyed");
}
}
==>BindServiceActivity.java
package com.wwj;
import android.app.Activity;
import android.app.Service;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class BindServiceActivity extends Activity {
/** Called when the activity is first created. */
private Button bind, unbind, getServiceStatus;
// 保持所启动的Service的IBinder对象
BindService.MyBinder binder;
// 定义一个ServiceConnection对象
private ServiceConnection conn = new ServiceConnection() {
// 当该Activity与Service断开连接时回调该方法
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
System.out.println("--Service Disconnected--");
//获取Service的onBind方法返回的MyBinder对象
}
// 当该Activity与Service连接成功后回调该方法
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
System.out.println("--Service Connected--");
binder = (BindService.MyBinder) service;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取程序界面中的bind,unbind,getServiceStatus按钮
bind = (Button)findViewById(R.id.bind);
unbind = (Button)findViewById(R.id.unbind);
getServiceStatus = (Button) findViewById(R.id.getServiceStatus);
//创建启动Service的Intent
final Intent intent = new Intent();
//为Intent设置Action
intent.setAction("com.wwj.BIND_SERVICE");
bind.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//绑定指定Service
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
});
unbind.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//解除绑定Service
unbindService(conn);
}
});
getServiceStatus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//获取并显示Service的count值
Toast.makeText(BindServiceActivity.this, "Service的count值为: " + binder.getCount(),
4000).show();
}
});
}
}
布局文件:main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定Service"
/>
<Button
android:id="@+id/unbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解除绑定"
/>
<Button
android:id="@+id/getServiceStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取Service状态"
/>
</LinearLayout>
注册文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wwj"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".BindServiceActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".BindService">
<intent-filter >
<action android:name="com.wwj.BIND_SERVICE"/>
</intent-filter>
</service>
</application>
</manifest>
项目运行效果:
分享到:
相关推荐
本文实例讲述了Android开发之Service用法。分享给大家供大家参考。具体分析如下: Service是一个生命周期较长而且没有界面的程序。 下面通过一个播放mp3的例子来学习。 先看MainActivity.java package ...
Android Service之bound实现
Android之Service实现比较大小
Android 的 Remote Service 开发实例RemoteService
Android 的 Remote Service 开发实例RemoteServiceClient~
Android开发之WebService介绍
Android 开发 Service讲解
Android Service下载文件
AccessibilityService无障碍服务库,一行代码启用,快速开发复杂的自动操作业务
一个录音机程序,简单的实现了广播,service等功能
Android 是Google开发的基于Linux平台的开源手机操作系统。它包括操作系统、用户界面和应用程序—— 移动电话工作所需的全部软件,而且不存在任何...【第一版第十五章】老罗Android开发视频--百度地图实战开发(10集)
android连接sqlservice驱动 android连接sqlservice驱动 android连接sqlservice驱动
service的启动,停止,绑定,取消绑定,以及IntentService的生命周期走向测试和IntentService与访问者信息通信过程,不清楚之处请参阅我的博客《Android开发四大组件之Service总结》
Android Native Service
Service 是一个可以在后台执行长时间运行操作而不提供用户界面的应用组件。服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行。...只要满足这两个情况之一,都可以考虑使用Service。
Android开发基础,讲述了Android开发环境的搭建、Android常用工具的使用和第一个Android应用程序的开发 第二篇 技术篇 第3章 Android中的资源访问 Android 中的资源访问,讲述了如何定义和访问Android中的外部...
Android 实现进程及Service常驻之MarsDaemon源码
《Android开发案例驱动教程》 配套代码。 注: 由于第12,13,14章代码太大,无法上传到一个包中。 这三节代码会放到其他压缩包中。 作者:关东升,赵志荣 Java或C++程序员转变成为Android程序员 采用案例驱动模式...