关于Android中传递数据的一些讨论?? ???? 在Android中编写过程序的开发人员都知道。在Activity、Service等组件之间传递数据(尤其是复杂类型的数据)很不方便。一般可以使用Intent来传递可序列化或简单类型的数据。看下面的代码。 注:如果经常使用static的Bitmap、Drawable等变量。可能就会抛出一个在Android系统中非常著名的异常(以前budget这个单词一直记不住什么意思,自从经常抛出这个异常后,这个单词终于烂熟于心了, ) ERROR/AndroidRuntime(4958): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget ??? 那么现在来看看如何使用这个Application Context。我们可以通过Context.getApplicationContext或Context.getApplication方法获得 Application Context。但要注意,我们获得的只是Context对象,而更理想的方法是获得一个类的对象。ok,说干就干,下面来定义一个类。 import? android.app.Application; public? class? MyApp? extends? Application ???? public? Bitmap getBitmap() ???? public? void? setBitmap(Bitmap bitmap) ??? 获得Bitmap对象的代码: ??? ImageView imageview? =? (ImageView)findViewById(R.id.ivImageView); ? 很好的方法。
???? Intent intent? =? new? Intent( this , Test. class );
???? intent.putExtra( " param1 " ,? " data1 " );
???? intent.putExtra( " intParam1 " ,? 20 );
???? startActivity(intent);
???? 这样就ok了。在当前Activity将两个值传到了Test中。但如果遇到不可序列化的数据,如Bitmap、InputStream等,intent就无能为力了。因此,我们很自然地会想到另外一种方法,静态变量。如下面的代码所示:
??? public? class? Product? extends? Activity
?? {
???????? public? static? Bitmap mBitmap;
??????????
?? }
??? 对于上面的代码来说,其他任何类可以直接使用Product中的mBitmap变量。这么做很easy、也很cool,但却very very wrong。我们千万不要以为Davlik虚拟机的垃圾回收器会帮助我们回收不需要的内存垃圾。事实上,回收器并不可靠,尤其是手机上,是更加的不可靠。 因此,除非我们要使自己的程序变得越来越糟糕,否则尽量远离static。
??? 如果不使用static,总得有方法来代替它(尽管我很喜欢public static,我相信很多人也喜欢它,但为了我们的程序,建议还是忍痛割爱吧),那么这个新的解决方案就是本文的主题,这就是Application Context,相当于Web程序的Application,它的生命周期和应用程序一样长(这个我喜欢)。
package? net.blogjava.mobile1;
import? android.graphics.Bitmap;
{
???? private? Bitmap mBitmap;
??? {
???????? return? mBitmap;
??? }
??? {
???????? this .mBitmap? =? bitmap;
??? }
????
}
??? 上面这个类和普通的类没什么本质的不同。但该类是Application的子类。对了,这就是使用Application Context的第一步,定义一个继承自Application的类。然后呢,就在这个类中定义任何我们想使其全局存在的变量了,如本例中的 Bitmap。下面还需要一个重要的步骤,就是在<application>标签中使用android:name属性来指定这个类,代码如 下:
< application? android:name =".MyApp"? android:icon ="@drawable/icon"? android:label ="@string/app_name" >?
?
</ application?
??? 接下来的最后一步就是向MyApp对象中存入Bitmap对象,或从MyApp对象中取出Bitmap对象了,存入Bitmap对象的代码如下:
??? MyApp myApp? =? (MyApp)getApplication();
????????
??? Bitmap bitmap? =? BitmapFactory.decodeResource( this .getResources(), R.drawable.icon);
????????
??? myApp.setBitmap(bitmap);
????????
??? MyApp myApp? =? (MyApp)getApplication();
????????
??? imageview.setImageBitmap(myApp.getBitmap());?
????
??? 上面两段代码可以在任何的Service、Activity中使用。全局的,哈哈。
详细解决方案
[转载]关于Android中传送数据的一些讨论 intent行不通,static不让用的时候用application
热度:22 发布时间:2016-05-01 19:49:29.0
相关解决方案
- 采取spring mvc和hibernate开发,application.xml的配置求详细分析
- Parse error in application web.xml,该如何解决
- JSP文件开头声明了是一个上载文件(contentType="application/x-download"),是不是就不能在上面执行javascript和html了
- android 读取byte[]中的元素解决方案
- android 标题栏兑现方式
- android 中Activity向BroadcastReceiver发送数据,该怎么解决
- Android 4.0 为什么模拟器老是提示小弟我谷歌拼音输入法已停止
- android:getSharedPreferences() 这是哪个类的方法解决思路
- android 怎么判断一个程序是否联网
- android 大量数据按周分组,该如何解决
- android RadioButton如何设置默认选中
- 急问:S60,eclipse,打开模拟器总是提示“unhandled exception.Close application?该如何解决
- java.lang.SecurityException: Application not
- NEC N820 Application Emulator 装上后,不有编译解决思路
- java.lang.SecurityException: Application not authorized to access the restricted API有关问题,
- ksoap2-android-这个包,连接webService怎么设置超时
- android 怎么重新设置锚点
- android UI界面设计解决方案
- android 图片对象获取的有关问题
- 关于Thinking in java中import static net.mindview.Print.失误
- android 怎么调用淘宝支付宝接口
- Android 沿袭InputMethodService自定义输入法
- Your project contains error(s), please fix them beforerunning your application.该怎么解决
- android 关于服务连接的疑义
- 关于Thinking in java中import static net.mindview.Print.疏失
- android 两个activity如何通信
- android 怎么实现对view的放大和缩小
- 一个关于 static 和final的程序 看不明白了 哪位高手知道呢
- 为什么虚拟串口安装了不能用 显示“Unable loading system driver ,the application will be eixt.”解决办法
- android 教程解决方法