Android--AIDL 크로스 애플 리 케 이 션 시작 및 닫 기 Service
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.