Intents

Explicit Intents

Explicit Intents specify the component to start by name (typically to start a component in your own app). When an explicit intent is created to start an activity or service, the system immediately starts the app component specified in the Intent object.

    Intent downloadIntent = new Intent(this, DownloadService.class);
    downloadIntent.setData(Uri.parse(fileUrl));
    startService(downloadIntent);



Implicit Intents

Implicit Intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. When an implicit intent is created to start an activity or service, the Android system finds the appropriate component(s) to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object.

If multiple intent filters are compatible, the system displays a dialog so the user can pick which app to use.

Using the "geo:" URI scheme for showing a location found on a Google Map. This super-handy intent is detailed in the "Common Intents" page of Android's developer site: http://developer.android.com/guide/components/intents-common.html#Maps

    // Map: "geo:latitude,longitude?z=zoom"
    geoLocation = Uri.parse("geo:" + lat + "," + lon + "?z=11");

// or
    // Map "geo:0,0?q=my+street+address"
    geoLocation = Uri.parse("geo:0,0?").buildUpon()
            .appendQueryParameter("q", location)
            .build();


    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(geoLocation);
    if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
        startActivity(intent);      // starts Map Application
    }



Sending/putExtra Custom Objects/data with Intents

To dispatch objects/data bundle along with intents, it must be Parcelable so that it can be passed around. (This is something similar to serializable). You may need to make your custom member fields (in non-standard data types) also parcelable.

public class MovieData implements Parcelable {
    private MovieData(Parcel in) {
        mId             = in.readString();
        mOriginalTitle  = in.readString();
        // additionally, need custom code to make ArrayList<String[]> movieReviews Parcelable.
    }
    public void writeToParcel(Parcel out, int flags) {
        out.writeString(mId);
        out.writeString(mOriginalTitle);
    }
} // MovieData


public class MainActivityFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MovieData movieData = mAdapter.getItem(position);
                detailActivityIntent.putExtra(String.valueOf(R.string.movie_data_key), movieData);
            }
        }
    }
} // MainActivityFragment


public class DetailActivityFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Intent intent = getActivity().getIntent();
        MovieData movieData = intent.getParcelableExtra(String.valueOf(R.string.movie_data_key));
        ((TextView) rootView.findViewById(R.id.movie_original_title))
                            .setText(movieData.getOriginalTitle());
    }
} // DetailActivityFragment




No comments:

Post a Comment