android学习笔记(四)

AIDL的理解与使用

跨应用启动服务

前提:已经准备好了一个应用的服务。

新建另一个应用程序。

MainActivity.java

package com.example.anotherapplication;

import android.content.ComponentName;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

        serviceIntent = new Intent();
//        通过setComponent来实现对另一个应用程序的服务的抓取
        //                参数(“包名”,“文件名”)   PS:全路径、显示启动
        serviceIntent.setComponent(new ComponentName("com.example.myapplication","com.example.myapplication.startService"));

        findViewById(R.id.start).setOnClickListener(this);
        findViewById(R.id.stop).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.start:
                startService(serviceIntent);
                break;
            case R.id.stop:
                stopService(serviceIntent);
                break;
        }

    }
}

xml视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.anotherapplication.MainActivity"
    android:orientation="vertical"
    >

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/start"
    android:text="启动服务"
    />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/stop"
        android:text="停止服务"
        />
</LinearLayout>
在模拟器上运行程序,在监控台中可以验证启动了另一个程序的服务。

跨应用绑定服务

基于前面的学习,我们知道绑定一个服务需要使用Binder。但是如何在多个程序之间(不知道程序里面的类)的情况下通信呢。

在之前代码的基础上进行改进:
AnotherApplication.java

package com.example.anotherapplication;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {

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

        serviceIntent = new Intent();
//        通过setComponent来实现对另一个应用程序的服务的抓取
        //                参数(“包名”,“文件名”)   PS:全路径、显示启动
        serviceIntent.setComponent(new ComponentName("com.example.myapplication","com.example.myapplication.startService"));

        findViewById(R.id.start).setOnClickListener(this);
        findViewById(R.id.stop).setOnClickListener(this);
        findViewById(R.id.bind).setOnClickListener(this);
        findViewById(R.id.bind).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.start:
                startService(serviceIntent);
                break;
            case R.id.stop:
                stopService(serviceIntent);
                break;
            case R.id.bind:
                bindService(serviceIntent,this, Context.BIND_AUTO_CREATE);
                break;
            case R.id.unbind:
                unbindService(this);
                break;
        }

    }

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        System.out.println("bind service");
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {

    }
}

App:
在App(应用程序)中新增AIDL,自动生成需要的代码。
startService:

package com.example.myapplication;

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

public class startService extends Service {
    public startService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
       return new IMyAidlInterface.Stub() {
           @Override
           public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

           }
       };
    }

    @Override
    public void onCreate() {
        super.onCreate();

        System.out.println("Service started");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        System.out.println("Service stoped");
    }
}

posted on 2017-09-28 11:03  I_noname  阅读(121)  评论(0编辑  收藏  举报

导航