Android--AIDL 크로스 애플 리 케 이 션 시작 및 닫 기 Service

5474 단어 AndroidNative기초
머리말
AIDL 은 Android 인터페이스 정의 언어 로 번 역 된 Android 인터페이스 Definition Language 라 는 줄 임 말이다.주로 스 레 드 간 통신 에 사용 되 는데 본 고 는 주로 서로 다른 응용 간 에 AIDL 통신 을 사용 하 는 것 을 예 로 들 어 AIDL 을 소개 한다.
AIDL 의 사용 은 AIDL 파일 형식 에 따라 분류 되 며,하 나 는 직렬 화 된 데이터 클래스 로 Parcelable 을 실현 해 야 하 며,다른 하 나 는 시스템 사용 으로 크로스 프로 세 스 통신 을 완성 할 수 있 도록 방법 인 터 페 이 스 를 정의 하 는 것 이다.
AIDL 은 기본적으로 JAVA 의 8 가지 기본 데이터 형식,String,CharSequence,List 형식,Map 형식 을 지원 합 니 다.
단순 데모
응용 프로그램:
MainActivity.java
package com.example.dpl.startservicefromanotherapp;

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

public class MainActivity extends AppCompatActivity {

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

    @Override
    protected void onDestroy() {
        super.onDestroy();
        intent=new Intent(this,MyService.class);
        stopService(intent);
    }
}

MyService.java
package com.example.dpl.startservicefromanotherapp;

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

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

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

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

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

다른 응용 프로그램 anotherApp
activity_main.xml




    

MainActivity.java

package com.example.anotherapp;

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 anotherIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btnStart).setOnClickListener(this);
        findViewById(R.id.btnStop).setOnClickListener(this);
        anotherIntent=new Intent();
        //  setComponent          (  ComponentName  )
        //ComponentName               
        anotherIntent.setComponent(new ComponentName("com.example.dpl.startservicefroman" +
                "otherapp","com.example.dpl.startservicefromanotherapp.MyService"));
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.btnStart:
                startService(anotherIntent);
                break;
            case R.id.btnStop:
                stopService(anotherIntent);
                break;
        }
    }
}

실행 결과:
09-17 21:54:16.694 2160-2160/? I/System.out: startService
09-17 21:55:36.901 2160-2160/com.example.dpl.startservicefromanotherapp I/System.out: stopService
09-17 21:56:00.000 2160-2160/com.example.dpl.startservicefromanotherapp I/System.out: startService
09-17 21:56:12.384 2160-2160/com.example.dpl.startservicefromanotherapp I/System.out: stopService
09-17 22:27:34.129 2160-2160/com.example.dpl.startservicefromanotherapp I/System.out: startService
09-17 22:27:36.091 2160-2160/com.example.dpl.startservicefromanotherapp I/System.out: stopService

좋은 웹페이지 즐겨찾기