Article source: blog.csdn.net/kieven2008/…

1. Message-based communication mechanism Intent — Boudle

**** has limited data types, such as Bitmap,InputStream, or LinkList.

Public static member variable;

3. Transfer based on external storage, File/Preference/ Sqlite. If you want to target third-party applications, Content Provider ** ** is required

4. IPC based communication mechanism

**** Transfers between context and Service, such as communication between activities and services, define the AIDL interface file.

Example: www.eoeandroid.com/thread-3624…

5. Based on the Application Context, examples are as follows:

The current Activity passes two values to Test. However, when it comes to non-serializable data, such as bitmaps, InputStreams, etc., intEnts can’t do anything about it. So it’s natural to think of another approach, static variables. The following code looks like this:


public  class  Product  extends  Activity

{

public  static  Bitmap mBitmap;         

}

For the above code, any other class can use the mBitmap variable in Product directly. It’s easy and cool, but it’s very very wrong. We should never assume that the Garbage collector of the Davlik virtual machine will help us collect unwanted memory garbage. In fact, recyclers are unreliable, especially on mobile phones. So, unless we want to make our programs worse and worse, stay as far away from static as possible.

Note: If static Bitmap, Drawable and other variables are often used. You might throw an exception that is very famous in Android (the word budget has become familiar since it was thrown so often).

ERROR/AndroidRuntime(4958): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget

If you don’t use static, you’ll have to find a way to replace it (although I love public static and I’m sure many people do, too, for the sake of our programs), then this new solution is the subject of this article. This is the Application Context. Equivalent to an Application for a Web Application, it has the same lifetime as an Application (which I like).

So now let’s see how to use this Application Context. We can through the Context. GetApplicationContext or Context. GetApplication method to obtain the Application Context. Note, however, that we are only getting the Context object, whereas ideally we are getting the object of a class. Ok, let’s go ahead and define a class.


package  net.blogjava.mobile1;

import  android.app.Application;

import  android.graphics.Bitmap;

public  class  MyApp  extends  Application

{

private  Bitmap mBitmap;

     public  Bitmap getBitmap()

{

return  mBitmap;

}

public void setBitmap(Bitmap bitmap) { this .mBitmap = bitmap; }}

The above class is not fundamentally different from ordinary classes. But this class is a subclass of Application. By the way, that’s the first step in using Application Context, defining a class that inherits from Application. From there, we define any variables in this class that we want to make globally available, such as bitmaps in this case. The next important step is to specify the class using the Android :name attribute in the <application> tag, as follows:


< application  android:name =”.MyApp”  android:icon =”@drawable/icon”  android:label =”@string/app_name” > 



</ application?

The last step is to either store or retrieve a Bitmap from MyApp. The code for storing a Bitmap is as follows:


MyApp myApp  =  (MyApp)getApplication();



Bitmap bitmap  =  BitmapFactory.decodeResource( this .getResources(), R.drawable.icon);



myApp.setBitmap(bitmap);

Get the code for the Bitmap object:

ImageView imageview = (ImageView)findViewById(R.id.ivImageView); MyApp myApp = (MyApp)getApplication(); imageview.setImageBitmap(myApp.getBitmap()); The above two pieces of code can be used in any Service or Activity. Global.

\

Reference:

1.flyvenus.net/?p=229

2. blog.csdn.net/nokiaguy/ar…

\