我想使用 AIDL 将字符串和位图传递给服务。该服务实现了这个 AIDL 方法:
void addButton(in Bundle data);
在我的例子中,Bundle 包含一个字符串和一个位图。
调用应用程序(客户端)有这段代码:
...
// 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();
}
...
在服务端,我有一个包含以下代码的 ButtonComponent 类:
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);
}
...
结果,按钮显示了正确的文本,我可以看到图标的空间,但没有绘制实际的位图(即文本左侧有一个空白空间)。
这样把Bitmap放到Bundle里对吗?
如果我应该使用 Parcel(相对于 Bundle),是否有任何方法可以在 AIDL 方法中维护单个“数据”参数以将文本和图标保持在一起?
附带问题:我如何决定使用 bundle 还是包裹?
非常感谢。
最佳答案
这是第二个问题的答案。
来源:http://www.anddev.org/general-f3/bundle-vs-parcel-vs-message-t517.html
A Bundle is functionally equivalent to a standard Map. The reason we didn't just use a Map is because in the contexts where Bundle is used, the only things that are legal to put into it are primitives like Strings, ints, and so on. Because the standard Map API lets you insert arbitrary Objects, this would allow developers to put data into the Map that the system can't actually support, which would lead to weird, non-intuitive application errors. Bundle was created to replace Map with a typesafe container that makes it explicitly clear that it only supports primitives.
A Parcel is similar to a Bundle, but is more sophisticated and can support more complex serialization of classes. Applications can implement the Parcelable interface to define application-specific classes that can be passed around, particularly when using Services. Parcelables can be more sophisticated than Bundles, but this comes at a cost of significantly higher overhead.
Bundle and Parcel are both data serialization mechanisms, and for the most part both are used when application code is passing data across processes. However, because Parcel is much higher overhead that Bundle, Bundles are used in the more common places like the onCreate method, where overhead must be as low as possible. Parcels are most commonly used to allow applications to define Services with logical APIs that can use application-meaningful classes as method arguments and return values. If we required Bundle there, it would result in really clunky APIs. You should in general still keep your Service APIs as simple as possible, because primitives will serialize more efficiently than custom Parcelable classes.
关于android - 将位图放入包中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7727916/
我喜欢使用Textile或Markdown为我的项目编写自述文件,但是当我生成RDoc时,自述文件被解释为RDoc并且看起来非常糟糕。有没有办法让RDoc通过RedCloth或BlueCloth而不是它自己的格式化程序运行文件?它可以配置为自动检测文件后缀的格式吗?(例如README.textile通过RedCloth运行,但README.mdown通过BlueCloth运行) 最佳答案 使用YARD直接代替RDoc将允许您包含Textile或Markdown文件,只要它们的文件后缀是合理的。我经常使用类似于以下Rake任务的东西:
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
在Ruby中,我可以将模块/类嵌套到其他模块/类中。我想要的是在文件或类中添加一些声明,以便能够通过短名称引用嵌套类,例如使用Inner获取Outer::Inner,就像在Java、C#等中一样。语法可能是这样的:moduleOuterclassInner;endclassAnotherInner;endendclassCimportOuter:[:Inner,:AnotherInner]deffInnerendend简单的实现可能是这样的:classClassdefimport(constants)@imported_constants=(@imported_constants||{
我正在熟悉一些JRuby代码,我希望能够在代码中放置一个断点并(像往常一样)从命令行运行,当它到达时进入调试器那一点。我可以在我的代码中添加一些东西来强制JRuby进入调试器吗?我尝试运行jruby-rdebugfoo.rb(而不是通常的jrubyfoo.rb),然后使用b设置断点bar.py:98,然后继续。但是每次出现异常时调试器都会停止,而且在到达我感兴趣的代码行之前似乎有很多异常。我希望能够将“break-into-debugger”在我的代码中添加行并运行jrubyfoo.rb并让调试器首先停止在该行。(即我正在寻找Ruby/JRuby等同于Python中的importpdb
我想邀请在这样的url中传递电子邮件的人:localhost:3000/invite_me/email@gmail.com我尝试了这个匹配,但它不起作用。match"/invite_me/:email"=>"application#invite_me",:constraints=>{:email=>'/.+@.+\..*/'}我收到以下错误:Noroutematches[GET]"/invite_me/waldyr.ar@gmail.com"rake路由输出:root/application#index/invite_me/:email(.:format)application#inv
这可能很简单,但似乎无法正常工作。我有以下提交按钮我想在按钮中放置一个icomoon图像但是我如何在按钮中获取span类..?谢谢 最佳答案 使用button_tag并以这种方式将跨度放在按钮中SendMessage 关于ruby-on-rails-将span放入f.submit,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22159698/
我正在使用Foggem生成预签名url。我可以成功执行此操作以获得对该文件的读取权限。这是我的做法:fog_s3=Fog::Storage.new({:provider=>'AWS',:aws_access_key_id=>key,:aws_secret_access_key=>secret})object_path='foo.wav'expiry=Date.new(2014,2,1).to_time.to_iurl=fog_s3.directories.new(:key=>bucket).files.new(:key=>object_path).url(expiry,path_sty
我正在运行grep方法以通过模式匹配进行过滤。这是示例代码。companies.grep/city/但是,ruby不允许我在railsView内的block中输入area_code。相反,我不得不像这样对其进行硬编码:companies.grep/miami/请记住,城市是一个变量。例如,city=miami但是,它会更新。你知道如何通过grep方法传递变量吗?此外,我尝试了companies.grep/#{city}/,但没有成功 最佳答案 companies.grep/#{city}/#orcompanies.grepRegex
我不知道这是否真的是好的ruby代码,但我想做的是将一个String分成两个单独的部分,并将这两个部分作为两个特定键的值。例如:name_a="HenryFillenger".split(/\s+/,2)name={:first_name=>name_a[0],:last_name=>name_a[1]}我想知道这是否可以通过一些ruby魔法在一行中完成。 最佳答案 您可以使用Hash[]和zip这样做:name=Hash[[:first_name,:last_name].zip("HenryFillenger".split
运行有问题或需要源码请点赞关注收藏后评论区留言一、利用ContentResolver读写联系人在实际开发中,普通App很少会开放数据接口给其他应用访问。内容组件能够派上用场的情况往往是App想要访问系统应用的通讯数据,比如查看联系人,短信,通话记录等等,以及对这些通讯数据及逆行增删改查。首先要给AndroidMaifest.xml中添加响应的权限配置 下面是往手机通讯录添加联系人信息的例子效果如下分成三个步骤先查出联系人的基本信息,然后查询联系人号码,再查询联系人邮箱代码 ContactAddActivity类packagecom.example.chapter07;importandroid