如果把Activity比喻为前台程序,那么service可以看做是一个后台程序。Service跟Activity一样也由Intent调用。

在工程里想要添加一个Service,先新建继承Service的类,然后到AndroidManifest.xml -> Application

中的Service标签中添加。

如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.service"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".MyService">
<intent-filter>
<action android:name="" />
</intent-filter>
</service>
</application> </manifest>

其中,第一个name是service的位置,包括完整的包名和service名,如果包名就是你定义的程序包名,也就是和gen目录下那个包的名字一样的话,直接".service名"就可以了。第二个name是你调用service时intent.setAction();中的参数,这个可以自己随便定义。

代码如下:

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/btnStartMyService"
android:text="StartMyService">
</Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/btnStopMyService"
android:text="StopMyService">
</Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/btnBindMyService"
android:text="BindMyService">
</Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/btnUnbindMyService"
android:text="UnbindMyService">
</Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/btnExit"
android:text="退出程序">
</Button>
</LinearLayout>

MyService.java:

package com.example.service;  

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log; public class MyService extends Service {
static public String ServiceState="";
@Override
public IBinder onBind(Intent arg0) {
Log.e("Service", "onBind");
ServiceState="onBind";
return null;
}
@Override
public boolean onUnbind(Intent intent){
super.onUnbind(intent);
Log.e("Service", "onUnbind");
ServiceState="onUnbind";
return false; }
@Override
public void onCreate(){
super.onCreate();
Log.e("Service", "onCreate");
ServiceState="onCreate";
}
@Override
public void onDestroy(){
super.onDestroy();
Log.e("Service", "onDestroy");
ServiceState="onDestroy";
}
@Override
public void onStart(Intent intent,int startid){
super.onStart(intent, startid);
Log.e("Service", "onStart");
ServiceState="onStart";
} }

MainActivity.java:

package com.example.service;  

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.util.Log;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity {
Button btnStartMyService,btnStopMyService,btnBindMyService,btnUnbindMyService,btnExit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartMyService=(Button)this.findViewById(R.id.btnStartMyService);
btnStartMyService.setOnClickListener(new ClickEvent()); btnStopMyService=(Button)this.findViewById(R.id.btnStopMyService);
btnStopMyService.setOnClickListener(new ClickEvent()); btnBindMyService=(Button)this.findViewById(R.id.btnBindMyService);
btnBindMyService.setOnClickListener(new ClickEvent()); btnUnbindMyService=(Button)this.findViewById(R.id.btnUnbindMyService);
btnUnbindMyService.setOnClickListener(new ClickEvent()); btnExit=(Button)this.findViewById(R.id.btnExit);
btnExit.setOnClickListener(new ClickEvent());
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.e("Activity","onDestroy");
} private ServiceConnection _connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
} @Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
class ClickEvent implements View.OnClickListener{ @Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,MyService.class);
if(v==btnStartMyService){
MainActivity.this.startService(intent);
}
else if(v==btnStopMyService){
MainActivity.this.stopService(intent);
}
else if(v==btnBindMyService){
MainActivity.this.bindService(intent, _connection, Service.BIND_AUTO_CREATE);
}
else if(v==btnUnbindMyService){
if(MyService.ServiceState=="onBind")//Service绑定了之后才能解绑
MainActivity.this.unbindService(_connection);
}
else if(v==btnExit)
{
MainActivity.this.finish();
} } }
}

最新文章

  1. PHP 调用webService方式
  2. AS 重装系统之后配置
  3. 基于android平台的出题软件---- 每日30题
  4. ActivityGroup、TabHost之子页面不刷新——getLocalActivityManager() 以及intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)用法
  5. jQuery.validator 详解二
  6. unfortunately launcher has stopped
  7. js里各浏览器解析XML,支持IE、火狐、Chrome等
  8. sphinx 配置实例
  9. Random.Next获取随即数
  10. Github开源Java项目(Disconf)上传到Maven Central Repository方法详细介绍
  11. log4j日志输出配置
  12. Spark编程进阶
  13. Windows 10 IoT Serials 8 – 如何改变UWP应用的目标平台
  14. centos7 mysql安装
  15. pycharm设置主题
  16. Git可视化教程——Git Gui的使用
  17. C#中委托,匿名函数,lamda表达式复习
  18. 进程控制(Note for apue and csapp)
  19. 【12c】root container 和 pdb 的一些差别
  20. sql With(NoLock),With(ReadPast)

热门文章

  1. Light OJ 1051 - Good or Bad
  2. XP与Win2003下网站配置
  3. HDU --2665
  4. OC语言中类目,延展,协议
  5. 神器 Sublime Text 3 的一些常用快捷键
  6. nginx主配置文件 在那找怎么打开
  7. freepbx 安装和配置
  8. Spring注入-Map
  9. 国内maven 仓库
  10. 关于封装unity3d的dll时候的进一步总结