您的位置:首页 > 移动开发 > Android开发

android 服务学习笔记(2)

2010-02-20 04:08 375 查看

Android 服务

1:认识服务 :

很多情况下,一些与用户很少需要产生交互的应用程序,我们一般让它们在后台运行就行了,而且在它们运行期间我们仍然能运行其他的应用。

为了处理这种后台进程,Android引入了Service的概念。Service在Android中是一种长生命周期的组件,它不实现任何用户界面。最常见的例子如:媒体播放器程序,它可以在转到后台运行的时候仍然能保持播放歌曲;或者如文件下载程序,它可以在后台执行文件的下载。
在做例子的时候,总有疑问android 如何在开启了服务以后知道我们停止的是哪一个服务呢???(系统有好多的服务),我感觉 系统或者自动 给每一个开启服务的根据我们的调用对象(活动)对象建立了一个记录 ,并分配了ID这样下次我们 用活动的方法 去停止服务的时候 系统就用这个我们提交的ID来注销服务。

2:如何使用服务

1:写一个继承自com.app.Service的类,在类里面重载里面的一些方法

自己写的类
代码
2:在配置文件里面注册刚才定义的服务 如下面的代码,代码写在挨着Application的地方
用的时候 要有个 服务连接类来和服务连接

3:运行

<service android:enabled="true" android:name=".MyService"></service>
界面


执行效果
单击 开启服务
代码:
case com.fzxy.zxy.ServiceDemo.R.id.btnStartService:
Intent i=new Intent(this,MyService.class);
//If this service is not already running, it will be instantiated and started
//(creating a process for it if needed); if it is running then it remains running
this.startService(i);
Toast.makeText(textMyService.this, "服务成功开启", Toast.LENGTH_SHORT).show();
Log.i(MyService.mTAG_MYSERVICE, "service started successfully !");



可以看见先输出的是 service started successfully ,在输出的 下面的 created 和 started的
,常理上应该 service started successfully 后输出但是 现在 他先输出了,我想这个原因是由于调用服务的时候系统初始化服务会花费一点时间的 。
单击 停止服务
代码
Intent ii=new Intent(this,MyService.class);
this.stopService(ii);
Toast.makeText(textMyService.this, "服务成功停止", Toast.LENGTH_SHORT).show();
Log.i(MyService.mTAG_MYSERVICE, "service stoped successfully !");
截图



可以看见上面的小企鹅也消失了。



同理 停止服务也是 service is destroyed 也是在后面输出的,
Service里面没有OnStop这个方法的
单击绑定
代码
Intent iii=new Intent(this,MyService.class);

this.bindService(iii, this.connection, Context.BIND_AUTO_CREATE);
Toast.makeText(textMyService.this, "绑定成功", Toast.LENGTH_SHORT).show();
Log.i(MyService.mTAG_MYSERVICE, "service binded successfully !");

截图



下面是解绑的输出
可以看见解绑的时候同时也执行了 destroyed方法



完整代码
package com.fzxy.zxy.ServiceDemo;

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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class textMyService extends android.app.Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//Toast.makeText(textMyService.this, "服务连接成功", Toast.LENGTH_SHORT).show();
Log.i(MyService.mTAG_MYSERVICE, "intent is at here!");
setContentView(com.fzxy.zxy.ServiceDemo.R.layout.textservice);

Button btnStart=(Button)findViewById(com.fzxy.zxy.ServiceDemo.R.id.btnStartService);
btnStart.setOnClickListener(this);
Button btnStop=(Button)findViewById(com.fzxy.zxy.ServiceDemo.R.id.btnStopService);
btnStop.setOnClickListener(this);
Button btnBind=(Button)findViewById(com.fzxy.zxy.ServiceDemo.R.id.btnBindService);
btnBind.setOnClickListener(this);
Button btnUnBind=(Button)findViewById(com.fzxy.zxy.ServiceDemo.R.id.btnUnBindService);
btnUnBind.setOnClickListener(this);
}
//定义两个变量 是否绑定服务和 一个 自定义服务的实例
private boolean misBoundService;
private MyService myService=null;

private ServiceConnection connection=new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
textMyService.this.myService=((MyService.MyBinder)service).getMyService();
//Toast.makeText(textMyService.this, "服务连接成功", Toast.LENGTH_SHORT).show();
Log.i(MyService.mTAG_MYSERVICE, "service is connected successfully !");
}

@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
textMyService.this.myService=null;
//Toast.makeText(textMyService.this, "服务连接成功", Toast.LENGTH_SHORT).show();
Log.i(MyService.mTAG_MYSERVICE, "service is connected successfully !");
}

};
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case com.fzxy.zxy.ServiceDemo.R.id.btnStartService:
Intent i=new Intent(this,MyService.class);
//If this service is not already running, it will be instantiated and started
//(creating a process for it if needed); if it is running then it remains running
this.startService(i);
Toast.makeText(textMyService.this, "服务成功开启", Toast.LENGTH_SHORT).show();
Log.i(MyService.mTAG_MYSERVICE, "service started successfully !");
break;
case com.fzxy.zxy.ServiceDemo.R.id.btnStopService:
Intent ii=new Intent(this,MyService.class);
this.stopService(ii);
Toast.makeText(textMyService.this, "服务成功停止", Toast.LENGTH_SHORT).show();
Log.i(MyService.mTAG_MYSERVICE, "service stoped successfully !");
break;
case com.fzxy.zxy.ServiceDemo.R.id.btnBindService:
Intent iii=new Intent(this,MyService.class);
//This function will throw SecurityException if you do not have permission to bind to the given service
//if this Context is an Activity that is stopped, the service will not be required to continue running
//until the Activity is resumed.
this.bindService(iii, this.connection, Context.BIND_AUTO_CREATE);
Toast.makeText(textMyService.this, "绑定成功", Toast.LENGTH_SHORT).show();
Log.i(MyService.mTAG_MYSERVICE, "service binded successfully !");
this.misBoundService=true;
break;
case com.fzxy.zxy.ServiceDemo.R.id.btnUnBindService:
if(this.misBoundService){
this.unbindService(this.connection);
this.misBoundService=false;
Toast.makeText(textMyService.this, "解绑成功", Toast.LENGTH_SHORT).show();
Log.i(MyService.mTAG_MYSERVICE, "service unbinded successfully !");
}
break;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: