Intent

Gallery intent tutorial

SDK Version: 
M3

Our goal is to start the users gallery application, allow him to select an image, and use the chosen image in our application.

API Level 3 required!

We will start the operation on a buttons onclick event, implemented as follows:

  1. private static Bitmap Image = null;
  2. private static Bitmap rotateImage = null;
  3. private ImageView imageView;
  4. private static final int GALLERY = 1;

  1. public void onClick(View v) {
  2.   imageView.setImageBitmap(null);
  3.   if (Image != null)
  4.     Image.recycle();
  5.   Intent intent = new Intent();
  6.   intent.setType("image/*");
  7.   intent.setAction(Intent.ACTION_GET_CONTENT);
  8.   startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY);
  9. }

In order to avoid out of memory errors, we must recycle the previous image when the user presses the button second times or after that, so the returned bitmap is stored as a member variable, so we still have a reference for it when it is needs to be recycled.

Communicating between running activities

SDK Version: 
M3

Starting a new activity from another and passing some data to it is a simple and basic thing in android. But if you want an already running activity to come to foreground, and pass data to it, it can be a bit tricky.

First of all by default if you call an activity with an intent, a new istance of that activity will be created and displayed, even if another instance is already running. To avoid this the activity must be flagged that, it should not be instantiated multiple times. To achieve this we will set the launchMode of the activity to singleTask in the AndroidManifest.xml

  1. <activity android:name="Activity1" android:launchMode="singleTask" android:label="@string/app_name">

This way when we call this activity using an intent, if there is an existing instance, the system will route the request to it. Hoever the onCreate method, where we usually process the passed extraData, will not run this time.

Calling system settings from an Android app - GPS example

SDK Version: 
M3
This tutorial shows how to redirect the user to a system settings screen asking to modify some settings the application depends on. We will make a specific example with GPS: The application can be used only if GPS is available.

The android systems GPS setting screen can be called just like any other Activities:

  1. startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);

To check GPS availability use the code code below: (the Activity must implement LocationListener)

Mastering Android Widget Development - Part3

SDK Version: 
M3

I just come to a new discovery regarding widgets. I was developing an appwidget, which - just like the widgets we are trying to make in this series of tutorials - and tried it out multiple phones. Unfortunatelly on one of our test phones it didn't function properly. For random intervals it stopped to refresh, and it din't responded to button presses on the widget, only after 1-2 minutes. After hard work I discivered the following:

Buttons on appwiget can have their onclick flunctionality thorough RemoteViews.setOnClickPendingIntent(). This method gets a PendingIntent ap parametes to bound to a button. PendingIntent's can have 3 types created with

getActivity(Context, int, Intent, int)


getBroadcast(Context, int, Intent, int)


and getService(Context, int, Intent, int).

Syndicate content