码迷,mamicode.com
首页 > 其他好文 > 详细

13:aidl\service详解

时间:2018-05-01 12:19:00      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:exception   method   string   des   引用   com   length   ini   show   

目录结构:(client端的包名路径和service必须相同)

1:client端:                                                  service端:

 

技术分享图片技术分享图片

 

2:client端AIDLTest.aidl

package com.example.aidl;
interface AIDLTest{
    String hello(String name); 
}

MainActivity:

package com.example.aidlclient;

import android.app.Activity;
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.os.RemoteException;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

import com.example.aidl.AIDLTest;


//如果你只是想要启动一个后台服务长期进行某项任务,那么使用startService便可以了。如果你还想要与正在运行的Service取得联系,那么有两种方法:一种是使用broadcast,另一种是使用bindService。前者的缺点是如果交流较为频繁,容易造成性能上的问题,而后者则没有这些问题。因此,这种情况就需要startService和bindService一起使用了。
public class MainActivity extends Activity {
    AIDLTest RemoteService;//监听服务
    private ServiceConnection mConnection = new ServiceConnection() {
        
        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            Log.d("yjm", "1111111111111111111111");
            // TODO Auto-generated method stub
            
        }
        
        @Override
        public void onServiceConnected(ComponentName arg0, IBinder arg1) {
            Log.d("yjm", "2222222222222222222");
            // TODO Auto-generated method stub
            RemoteService = AIDLTest.Stub.asInterface(arg1);
            try {
                String string = RemoteService.hello("aaaaaaaaaaaaaa");
                Toast.makeText(MainActivity.this, string, Toast.LENGTH_LONG).show();
                Log.d("yjm", "QQQQQQQ"+string);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("yjm", "DDDDDDDDDDDD");
        initservice();
    }
    
    //连接服务
    public void initservice(){
        /* Intent i = new Intent();
         i.setAction("android.intent.action.AIDLService");*/
        //Intent i = new Intent(MainActivity.this, AIDLTest.class);
        //Intent i = new Intent("android.intent.action.AIDLService");
         Intent i = new Intent();
         i.setAction("android.intent.action.AIDLService");
         i.setPackage("com.example.aidl.AIDLService");
         try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         boolean ret = bindService(i, mConnection, Context.BIND_AUTO_CREATE);
    }
    
     //断开服务
    private void releaseService() {
        unbindService(mConnection);
        mConnection = null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

 

3:service端AIDLTest.aidl

package com.example.aidl;
interface AIDLTest{
    String hello(String name); 
}

 MainActivity:

package com.example.aidlservice;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intent = new Intent(MainActivity.this, AIDLService.class);
        startService(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
     @Override  
        public void onDestroy() {  
            super.onDestroy();  
            stopService(intent);
        }  

}

 

 AIDLService:

package com.example.service;

import com.example.aidl.AIDLTest;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

public class AIDLService extends Service{

    @Override
    public IBinder onBind(Intent arg0) {
        /*return new AIDLTest.Stub() {
            
            @Override
            public String hello(String name) throws RemoteException {
                // TODO Auto-generated method stub
                return "hello"+name;
            }
        };*/
        return mBinder;
    }
    
    //一个类,继承了Binder,那么它的对象就可以被远程的进程使用了(前提是远程进程获取了这个类的对象【对象的引用】
    private final AIDLTest.Stub mBinder = new AIDLTest.Stub() {
        
        @Override
        public String hello(String name) throws RemoteException {
            // TODO Auto-generated method stub
            return "hello"+name;
        }
    };

}

 

13:aidl\service详解

标签:exception   method   string   des   引用   com   length   ini   show   

原文地址:https://www.cnblogs.com/wnpp/p/8975645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!