首页 > 其他分享 >Android学习笔记(五三):服务Service(下)- Remote Service

Android学习笔记(五三):服务Service(下)- Remote Service

时间:2023-04-09 13:01:25浏览次数:42  
标签:showInfo Remote Service void private stub client Android


之前所谈的Service属于Local Service,即Service和Client在同一进程内(即同一application内),Service的生命周期服从进程的生命周期。在实际应用上,有时希望Service作为后台服务,不仅被同一进程内的activity使用,也可被其他进程所使用,针对这种情况,需要采用bindService,也就是Remote Service的方式。

在Android中,不同app属不同进程(process),进程是安全策略的边界,一个进程不能访问其他进程的存储(例如采用ContentProvider)。在Remote Service中将涉及进程间通信,也就是通常讲的IPC(interprocess commnication),需要在进程A和进程B之间建立连接,以便进行相互的通信或数据传递 。



Android学习笔记(五三):服务Service(下)- Remote Service_eclipse

Android提供AIDL(Android Interface Definition Language)工具帮助IPC之间接口的建立,大大地简化了开发者视图。右示意图仅用于帮助理解代码。通过下面的步骤实现client和service之间的通信:

【1】定义AIDL接口 ,Eclipse将自动为Service建立接口IService
【2】Client连接Service,连接到IService暴露给Client的Stub,获得stub对象;换句话,Service通过接口中的Stub向client提供服务,在IService中对抽象IService.Stub具体实现。 
【3】Client和Service连接后,Client可向使用本地方法那样,简单地直接调用IService.Stub里面的方法。

下面的例子给出client从提供定时计数的Remote Service,称为TestRemoteService,中获得服务的例子。

步骤1:通过AIDL文件定义Service向client提供的接口,ITestRemoteService.aidl文件如下

package com.wei.android.learning.part5; 
 interface ITestRemoteService { 
     int getCounter(); 
}

我们在src的目录下添加一个I<ServiceClassName>.aidl文件,语法和java的相同。在这个例子中Service很简单,只提供计数器的值,故在接口中我们定义了int getCounter( )。

AIDL文件很简单,Eclipse会根据文件自动生成相关的一个java interface文件,不过没有显示出来,如果直接使用命令行工具会帮助生成java文件。

步骤2:Remote Service的编写,通过onBind(),在client连接时,传递stub对象。 TestRemoteService.java文件如下:

/* Service提供一个定时计数器,采用Runnable的方式实现,复习一下Android学习笔记(三一):线程:Message和Runnable中的例子3。为了避免干扰注意力,灰掉这部分代码。此外,我们提供showInfo(),用于跟踪Service的运行情况,这部分也灰掉。*/
 public class TestRemoteService extends Service{ 
    private Handler serviceHandler = null;
     private int counter = 0; 
     private TestCounterTask myTask = new TestCounterTask();  
     
     public void onCreate() {  
         super.onCreate(); 
         showInfo("remote service onCreate()");
     }  

     public void onDestroy() { 
         super.onDestroy(); 
         serviceHandler.removeCallbacks(myTask); //停止计数器
        serviceHandler = null; 
         showInfo("remote service onDestroy()");
     }  

     public void onStart(Intent intent, int startId) { 
        // 开启计数器 
        super.onStart(intent, startId); 
        serviceHandler=new Handler(); 
         serviceHandler.postDelayed(myTask, 1000); 
         showInfo("remote service onStart()"); 
     } 

    //步骤2.1:具体实现接口中暴露给client的Stub,提供一个stub inner class来具体实现。 
     private ITestRemoteService.Stub stub= new ITestRemoteService.Stub() {
        //步骤2.1:具体实现AIDL文件中接口的定义的各个方法。
        public int getCounter() throws RemoteException {  
showInfo("getCounter()");
             return counter; 
         } 
     };  
      
    //步骤2.2:当client连接时,将触发onBind(),Service向client返回一个stub对象,由此client可以通过stub对象来访问Service,本例中通过stub.getCounter()就可以获得计时器的当前计数。在这个例子中,我们向所有的client传递同一stub对象。 
    public IBinder onBind(Intent arg0) {          showInfo("onBind() " + stub); //我们特别跟踪了stub对象的地址,可以在client连接service中看看通过ServiceConnection传递给client
        return stub; 
    }  

     /* 用Runnable使用定时计数器,每10秒计数器加1。 */ 
    private class TestCounterTask implements Runnable{
         public void run() {  
             ++ counter; 
             serviceHandler.postDelayed(myTask,10000); 
             showInfo("running " + counter); 
         } 
     }  
     /* showInfo( ) 帮助我们进行信息跟踪,更好了解Service的运行情况 */
    private void showInfo(String s){ 
         System.out.println("[" +getClass().getSimpleName()+"@" + Thread.currentThread().getName()+ "] " + s);
     } 
 }

步骤3:Client和Service建立连接,获得stub,ServiceTest4.java代码如下


Android学习笔记(五三):服务Service(下)- Remote Service_interface_02

public class ServiceTest4 extends Activity{
     private ITestRemoteService remoteService = null;  //步骤3.1 定义接口变量     private boolean isStarted = false;
    private CounterServiceConnection conn = null;     //步骤3.1 定义连接变量,实现ServiceConnection接口       
     protected void onCreate(Bundle savedInstanceState) {  
         … … //5个button分别触发startService( ),stopService( ) , bindService( ), releaseService( )和invokeService( ),下面两行,一行是显示从Service中获得的计数值,一行显示状态。    
    } 

     private void startService(){ 
         Intent i = new Intent();  
         i.setClassName("com.wei.android.learning", "com.wei.android.learning.part5.TestRemoteService"); //我的这个包里面还有层次,如*.part1、*.part2,etc
        startService(i); //和之前的local service一样,通过intent开启Service,触发onCreate()[if Service没有开启]->onStart()        isStarted = true;  
         updateServiceStatus();  
     } 
     private void stopService(){ 
         Intent i = new Intent(); 
         i.setClassName("com.wei.android.learning","com.wei.android.learning.part5.TestRemoteService");
         stopService(i); //触发Service的 onDestroy()[if Service存在]        isStarted = false; 
         updateServiceStatus(); 
     }    
    //步骤3.3:bindService( )通过一个实现ServiceConnection接口的类于Service之间建立连接,注意到里面的参数Context.BIND_AUTO_CREATE,触发onCreate()[if Service不存在] –> onBind()。    private void bindService(){  
        if(conn == null){ 
             conn = new CounterServiceConnection();             Intent i = new Intent(); 
             i.setClassName("com.wei.android.learning","com.wei.android.learning.part5.TestRemoteService");
            bindService(i, conn,Context.BIND_AUTO_CREATE);            updateServiceStatus(); 
        } 
    }

Android学习笔记(五三):服务Service(下)- Remote Service_service_03


  

private void releaseService(){ 
         if(conn !=null){ 
             unbindService(conn); //断开连接,解除绑定            conn = null; 
            updateServiceStatus(); 
         } 
     } 
     private void invokeService(){  
         if(conn != null){ 
             try{ 
                 Integer counter = //一旦client成功绑定到Service,就可以直接使用stub中的方法。                 TextView t = (TextView)findViewById(R.id.st4_notApplicable);
                 t.setText("Counter value : " + Integer.toString(counter));
             }catch(RemoteException e){ 
                 Log.e(getClass().getSimpleName(),e.toString()); 
             } 
         } 
     } 
    //步骤3.2 class CounterServiceConnection实现ServiceConnection接口,需要具体实现里面两个触发onServiceConnected()和onServiceDisconnected()     private class CounterServiceConnection implements ServiceConnection{
         @Override 
         public void onServiceConnected(ComponentName name, IBinder service) {
             // 从连接中获得stub对象,根据我们的跟踪,remoteService就是service中的stub对象 
            remoteService = ITestRemoteService.Stub.asInterface(service);             showInfo("onServiceConnected()" + remoteService);
         } 

         @Override 
         public void onServiceDisconnected(ComponentName name) { 
             remoteService = null; 
            updateServiceStatus(); 
             showInfo("onServiceDisconnected"); 
         } 
     } 
    private void updateServiceStatus() {  
         TextView t = (TextView)findViewById( R.id.st4_serviceStatus); 
         t.setText( "Service status: "+(conn == null ? "unbound" : "bound")+ ","+ (isStarted ? "started" : "not started"; ));     
       } 
     private void showInfo(String s){  
         System.out.println("[" +getClass().getSimpleName()+"@" + Thread.currentThread().getName()+ "] " + s);
     } 
 }


标签:showInfo,Remote,Service,void,private,stub,client,Android
From: https://blog.51cto.com/u_9877302/6178781

相关文章

  • Android学习笔记(四九):通过Content Provider访问数据
    在上次笔记中,我们编写了自己的Provider,这次笔记,我们将通过ContentProvider的Uri接口对数据进行访问,重写Android学习笔记(四二)中例子。在这里我们不在充分描述相关UI如何编写,可以到笔记(四二)中详细查看,重点讲述如何实现数据的访问。读取信息读取信息方式,在笔记(四七)中已经介绍,代码如下......
  • Android学习笔记(四八):提供自己的Content Provider
    在上一次的学习中,采用了原生的内容提供者Contact,Contact有多层映射关系,比较复杂,并非作为小例子的好选择,在本次学习中,我们将学习如何建立ContentProvider,并通过Uri进行增删改查。如果应用的数据只需自己使用,并不需要contentprovider,相反避免这样做,可直接访问数据;但是若希望数据可......
  • 小故事:开发者对Android权限的看法
    Wei:投诉一下,App的log.txt放在系统根目录下,这比较过分某L:原来还没改过来……去年就反映过这个问题了。Wei:升级App后,我向其他人发短信,腾讯手机管家报App要读取短信。为什么要监听发短信?可能应为某些功能,要监听来自某号的特殊短信,但为何要监听发送短信?某登:那个流量与内容监控的项目,是......
  • Pro Android学习笔记(四六):Dialog(3):对话框弹对话框
    提示框的按钮Help,将触发弹出新的帮助提示框。帮助提示框的实现帮助提示框的实现很简单,利用重写onCreateView()的方式,点击按钮是执行dismiss(),关闭对话框即可。代码不在此重复。dialogfragment的关闭有两种方式,一种是在dialogfragment中直接执行dismiss(),我们来看看DialogFragmen......
  • Pro Android学习笔记(四五):Dialog(2):DialogFragment
    DialogFragment的实例newInstance()已经在上一次学习笔记中实现。我们创建dialog的UI,可以通过重写DialogFragment的两个函数当中的一个来实现,这两个函数是onCreateView()和onCreateDialog(),前者返回view,后者返回dialog,如同通过AlertDialog.Builder构造一样。重写onCreateView()重写......
  • Pro Android学习笔记(四四):Dialog(1):触发Dialog
    Android提供alert、prompt、pick-list,单选、多选,progress、time-picker和date-picker对话框,并提供自定义的dialog。在Android3.0后,dialog基于fragment,并对之前版本提供兼容支持库,也就是说对于开发者而言,dialog是基于DialogFragment的,但此时需要在应用中加入相关的兼容库。和Window......
  • Pro Android学习笔记(二六):用户界面和控制(14):RelativeLayout
    相对布局:RelativeLayoutRelativeLayout也是非常常用的布局,能够精确对控件的位置进行网格对齐,可以设置在控件与其他控件的相对位置,以及控件在容器中的位置。缺省控件的位置为最上面还最左边。下面结合一个例子来进行解说。<?xmlversinotallow="1.0"encoding="utf-8"?><Relativ......
  • Android遇到内存泄漏和性能优化,需要采取以下措施
    内存泄漏:a.使用内存分析工具,如AndroidStudio的MemoryProfiler或LeakCanary,找出内存泄漏的位置,并修复代码。b.避免在Activity或Fragment中使用静态变量或单例模式,因为它们可能会持有对Activity或Fragment的引用,并导致内存泄漏。c.及时释放不再使用的资源,如关闭文件、释放......
  • Mac M1安装android sdk
    我这边是MacBookproM1芯片的电脑,需要安装androidsdk我在网上找了一圈,没有找到合适的sdk,要么是版本太老,要么是其他原因,后来我直接通过我的idea中进行下载,idea中搜索androidsdk即可下载edit进行安装,之后等待,好了之后进入此路径中,发现adb--version正常了主题你如果在网......
  • Android学习笔记(五二):服务Service(中)- 继承Service类
    通过IntentService的继承类实现命令触发的服务,也可以直接通过Service的继承类来实现。在IntentService中的例子,我们增加了StopService()的方式,用于试验。在实际应用中,IntentService常用于一次性运行,自动结束的情况,不需要人工停止干预。对于需要人工干预的停止的,长时间(或无限制)运行......