Some discussions about data transmission in Android

Source: Internet
Author: User
This article is original. If you need to reprint it, please indicate the author and the source. Thank you!

Written in AndroidProgramDevelopers all know. It is inconvenient to transmit data (especially complex data) between components such as activity and service. Generally, intent can be used to transmit serialized or simple data. See the followingCode.

Intent intent = New Intent ( This , Test. Class );
Intent. putextra ( " Param1 " , " Data1 " );
Intent. putextra ( " Intparam1 " , 20 );
Startactivity (intent );

In this case, OK. In the current activity, two values are passed to test. However, if you encounter non-serializable data, such as bitmap and inputstream, intent will be powerless. Therefore, we naturally think of another method, static variables. The following code is used:

Public Class Product Extends Activity
{
Public Static Bitmap mbitmap;

}

For the above Code, any other class can directly use the mbitmap variable in product. This is easy and cool, but very wrong. We should never think that the garbage collector of the davlik virtual machine will help us reclaim unwanted memory garbage. In fact, recyclers are unreliable, especially on mobile phones. Therefore, unless we want to make our program worse and worse, we should try to stay away from static.

Note: static bitmap, drawable, and other variables are often used. It may throw a very famous exception in the Android system (the word "budget" has never been remembered. Since this exception is often thrown, the word has finally become quite familiar ,)

Error/androidruntime (4958): caused by: Java. Lang. outofmemoryerror: bitmap size exceeds VM budget

If you do not use static, you must have a method to replace it (although I like public static very much, I believe many people like it, but for our program, we suggest you stick to it ), this new solution is the topic of this article. This is the application context, which is equivalent to the application of a web program. its lifecycle is as long as that of an application (which I like ).

Now let's take a look at how to use this application context. You can use context. getapplicationcontext or context. getapplication to obtain the application context. Note that we only obtain the context object, and the more ideal method is to obtain the object of a class. OK, just do it. The following defines a class.

Package Net. blogjava. mobile1;

ImportAndroid. App. Application;
ImportAndroid. Graphics. Bitmap;

Public ClassMyAppExtendsApplication
{
PrivateBitmap mbitmap;

PublicBitmap getbitmap ()
{
ReturnMbitmap;
}

Public VoidSetbitmap (Bitmap bitmap)
{
This. Mbitmap=Bitmap;
}

}

The above class is essentially different from the ordinary class. However, this class is a subclass of application. By the way, this is the first step to use application context to define a class that inherits from the application. Then, define any variables in this class that we want to make global, such as bitmap in this example. An important step is to use the Android: Name attribute in the <Application> tag to specify this class. The Code is as follows:

< Application Android: Name = ". MyApp" Android: icon = "@ Drawable/icon" Android: Label = "@ String/app_name" >

</ Application?

The next step is to save the bitmap object to the MyApp object or retrieve the bitmap object from the MyApp object. The code for storing the bitmap object is as follows:

MyApp = (MyApp) getapplication ();

Bitmap bitmap = Bitmapfactory. decoderesource ( This . Getresources (), R. drawable. Icon );

MyApp. setbitmap (Bitmap );

Get bitmap object code:Imageview=(Imageview) findviewbyid (R. Id. ivimageview );

MyApp=(MyApp) getapplication ();

Imageview. setimagebitmap (MyApp. getbitmap ());


The above two pieces of code can be used in any service or activity. Global, haha.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.