AIDL을 사용하여 문자열과 비트 맵을 서비스에 전달하려고합니다. 이 서비스는 다음 AIDL 메소드를 구현합니다.비트 맵을 번들로 묶기
void addButton(in Bundle data);
필자의 경우 Bundle에는 String과 Bitmap이 있습니다. 이 코드와 ButtonComponent 클래스가 서비스 종료에
...
// Add text to the bundle
Bundle data = new Bundle();
String text = "Some text";
data.putString("BundleText", text);
// Add bitmap to the bundle
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.myIcon);
data.putParcelable("BundleIcon", icon);
try {
myService.addButton(data);
} catch (RemoteException e) {
Log.e(TAG, "Exception: ", e);
e.printStackTrace();
}
...
:
호출 응용 프로그램 (클라이언트)이 코드가
public final class ButtonComponent implements Parcelable {
private final Bundle mData;
private ComponComponent(Parcel source) {
mData = source.readBundle();
}
public String getText() {
return mData.getString("BundleText");
}
public Bitmap getIcon() {
Bitmap icon = (Bitmap) mData.getParcelable("BundleIcon");
return icon;
}
public void writeToParcel(Parcel aOutParcel, int aFlags) {
aOutParcel.writeBundle(mData);
}
public int describeContents() {
return 0;
}
}
ButtonComponent를 생성 한 후, 서비스가 생성 ButtonComponent 객체의 텍스트 및 아이콘을 사용하는 버튼 :
...
mInflater.inflate(R.layout.my_button, aParent, true);
Button button = (Button) aParent.getChildAt(aParent.getChildCount() - 1);
// Set caption and icon
String caption = buttonComponent.getText();
if (caption != null) {
button.setText(caption);
}
Bitmap icon = buttonComponent.getIcon();
if (icon != null) {
BitmapDrawable iconDrawable = new BitmapDrawable(icon);
button.setCompoundDrawablesWithIntrinsicBounds(iconDrawable, null, null, null);
}
...
As 결과는 버튼이 올바른 텍스트로 표시되고 아이콘 공간을 볼 수 있지만 실제 비트 맵은 그려지지 않습니다 (예 : 텍스트의 왼쪽에 빈 공간이 있음).
이 방법으로 비트 맵을 번들에 넣는 것이 맞습니까?
내가 Parcel (vs a Bundle)을 사용해야한다면 AIDL 메서드에서 단일 'data'인수를 유지하여 텍스트와 아이콘을 함께 유지할 수 있습니까?
사이드 질문 : 번들 대 소포를 어떻게 사용합니까?
감사합니다.
감사합니다. 내 시나리오에서는 비트 맵이 ~ 900 바이트인데, 이는 꽤 큰 크기이며 서비스의 APK에 모든 것을 포함시키지 않으려합니다 (많은 다른 클라이언트가있을 수 있음). 고유 한 ID를 사용하면 클라이언트와 서비스 패키지 모두 실제 PNG ...가 포함될 것입니다. 맞습니까? – rippeltippel
모든 이미지는'/ res/drawable' 디렉토리에 있어야합니다. –