Android学习笔记之AIDL

Android学习笔记之AIDL

一.通过Intent跨应用Service通信

在StartServiceFromAnotherApp中
在MainActivity中

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//      startService(new Intent(this, AppService.class));
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
//      stopService(new Intent(this, AppService.class));
    }
    @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);
        reture true;
    }
}

创建一个AppService

public class AppService extends Service
{
    @Override
    public IBinder onBind(Intent intent)
    {
        return null;        
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        System.out.println("Service started");  
    }

    @Override
    public void onDestroy()
    {
        System.out.println("Service destroyed");
        super.onDestroy();
    }
}

在另外一个应用中

public class MainActivity extends Activity implements OnClickListener, ServiceConnection
{
    private Intent serviceIntent;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        serviceIntent = new Intent();
        serviceIntent.setComponent(new ComponentName("com.example.startservicefromanotherapp", "com.example.startservicefromanotherapp.AppService"));


        findViewById(R.id.btnStartService).setOnClickListener(this);
        findViewById(R.id.btnStopService).setOnClickListener(this);

    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.btnStartService:          
                startService(serviceIntent);
                break;
            case R.id.btnStopService:
                stopService(serviceIntent);
                break;  

        }
    }


    @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;
    }
}

通过intent的setComponent方法实现了夸应用通信。

二.通过.aldl文件来实现夸应用绑定并通信

.aldl文件中如下输入代码,即声明来一个接口,会自动生成一个.java文件,我们不要去编辑那个java文件。

package com.example.startservicefromanotherapp;

interface IAppServiceRemoteBinder
{
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString);

    void setData(String data)
}

在StartServiceFromAnotherApp中,注释掉startService,stopService

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

//      startService(new Intent(this, AppService.class));
}

@Override
protected void onDestroy()
{
    super.onDestroy();
//      stopService(new Intent(this, AppService.class));
}

在public IBinder onBind(Intent intent)中实现接口

public class AppService extends Service
{
    private String data = "默认数据";
    private boolean running = false;
    @Override
    public IBinder onBind(Intent intent)
    {
        return new IAppServiceRemoteBinder.Stub()
        {

            @Override
            public void basicTypes(int anInt, long aLong, boolean aBoolean,
                    float aFloat, double aDouble, String aString)
                    throws RemoteException
            {

            }

            @Override
            public void setData(String data) throws RemoteException
            {
                AppService.this.data = data;
            }

        };
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        System.out.println("Service started");

        new Thread()
        {
            public void run() 
            {
                super.run();
                running = true;
                while (running)
                {
                    System.out.println(data);

                    try
                    {
                        Thread.sleep(1000);
                    } catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            };
        }.start();
    }

    @Override
    public void onDestroy()
    {
        System.out.println("Service destroyed");
        super.onDestroy();
    }
}

在AnotherApp中

public class MainActivity extends Activity implements OnClickListener, ServiceConnection
{
    private Intent serviceIntent;
    private EditText etInput;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        serviceIntent = new Intent();
        serviceIntent.setComponent(new ComponentName("com.example.startservicefromanotherapp", "com.example.startservicefromanotherapp.AppService"));

        etInput = (EditText) findViewById(R.id.etInput);

        findViewById(R.id.btnStartService).setOnClickListener(this);
        findViewById(R.id.btnStopService).setOnClickListener(this);
        findViewById(R.id.btnBindService).setOnClickListener(this);
        findViewById(R.id.btnUnbindService).setOnClickListener(this);
        findViewById(R.id.btnSync).setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.btnStartService:          
                startService(serviceIntent);
                break;
            case R.id.btnStopService:
                stopService(serviceIntent);
                break;  
            case R.id.btnBindService:
                bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);
                break;  
            case R.id.btnUnbindService:
                unbindService(this);
                binder = null;
                break;  
            case R.id.btnSync:
                if(binder != null)
                {
                    try
                    {
                        binder.setData(etInput.getText().toString());
                    } catch (RemoteException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                break;  
        }
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service)
    {
        System.out.println("Bind Service");
        System.out.println(service);

        //binder = (IAppServiceRemoteBinder) service;
        binder = IAppServiceRemoteBinder.Stub.asInterface(service);
    }
    @Override
    public void onServiceDisconnected(ComponentName name)
    {
        // TODO Auto-generated method stub

    }

    private IAppServiceRemoteBinder binder = 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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值