Android学习之Service

来源:互联网 发布:多益网络制作人 编辑:程序博客网 时间:2024/05/29 03:22

早前就开始看Android,后来由于学校毕业的诸多事宜就耽误了一些时间,后来到公司工作开始着手Android开发,虽然是新人还没有开始什么实际意义的开发,但是对于Android这方面还是很感兴趣的。现在想想,对于安卓都没有系统的学习过。于是有时间还是好好学习下。
这些天看了关于android service方面的知识,在这里做个笔记,留下点痕迹,方面之后自己看看吧。
大笑

服务(Service),有这么两句话的定义”A Service is not a separate process ; A Serivce is not a thread.“。在android中service和activity是一个级别的,只能后台运行,可以与其他组件进行交互,没有用户界面。我们可以将之想象成windows服务或unix服务。
在android中支持两种类型的服务:本地服务和远程服务。
  1. 本地服务无法供在设备上运行的其他程序访问。一般而言,这些服务仅支持承载该服务的应用程序。
  2. 远程服务除了可以承载服务的应用程序的访问,还可以从其他程序访问。远程服务使用AIDL(android interface definnition languaage)想客户端定义自身。

这个应该很好理解,简单说就是本地服务就是自个用用,远程服务就大家一起用用。

实现服务的方法:

  1.     编写一个类扩展android.app.Serivce并实现onBind()方法,创建服务;
    public class MyService extends Service{    @overridepublic void onCreate(){//调用一次,启动时调用.....}public void onStart(){//可多次调用...}public void onDestory(){...}//上面三个方法都是系统自己调用的,不应该直接调用他们。她们的顺序也是服务执行的顺序。public void onBind(Intent intent){         return null;}}
  2.     将一条服务定义项添加到AndroidManifest.xml中。
    <service android :name=”MyService" />

    实现本地服务的例子:
    /** *  */package com.hdf.service;import android.R;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;/** * @author HuaDeFei * */public class BackGroudService extends Service {private NotificationManager nm;//消息传送//本地服务这个方法不用,故return null@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {// TODO Auto-generated method stubnm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);//获取消息showNotificationMsg("starting service....");//创建线程,在onCreeat方法中创建,因为此方法只调用一次Thread th = new Thread(null , new ServiceWorker() , "BackGroundService");th.start();}class ServiceWorker implements Runnable{@Overridepublic void run() {// 这里可以执行相关操作,如HTTP协议,数据库操作等...Log.i("here", "here");}}@Overridepublic void onDestroy() {// TODO Auto-generated method stubshowNotificationMsg("stoping serice....");super.onDestroy();}@Overridepublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubsuper.onStart(intent, startId);}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubLog.i("MyConnectionService","recivied start id "+ startId+":"+intent );return START_STICKY;}public void showNotificationMsg(String message){Notification notification = new Notification(R.layout.icon, message, System.currentTimeMillis());//这个可以自己设定图片之类的PendingIntent pintent = PendingIntent.getActivity(this, 0, new Intent(this , ServiceActivity.class), 0);notification.setLatestEventInfo(this, "BackGound Service", message, pintent);nm.notify(2,notification);//这个是调用notifity()方法时将一个唯一ID传给通知管理器。这个值自己定。}}


    package com.hdf.service;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;import com.hdf.R;public class ServiceActivity extends Activity implements OnClickListener {private Button b1,b2;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.service);b1 = (Button)findViewById(R.id.button1);b2 = (Button)findViewById(R.id.button2);b1.setOnClickListener(this);b2.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubint id = v.getId();switch(id){case R.id.button1:startService(new Intent(this , BackGroudService.class));//开始服务break;case R.id.button2:stopService(new Intent(this , BackGroudService.class));//结束服务break;}}}

    这里我xml文件就不给出了,很简单就是添加两个按钮。当然最后你不要忘了在AndroidManifext.xml注册服务哦。

    当然这里只是简单实现了本地服务,至于远程服务,貌似就哟点复杂了,so,继续学习......

原创粉丝点击