Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
swdev:android:start [2018/05/01 08:12] smayr [Implicit Intent] |
swdev:android:start [2018/05/22 14:15] (current) smayr [Resources] |
||
---|---|---|---|
Line 5: | Line 5: | ||
* [[https:// | * [[https:// | ||
* [[https:// | * [[https:// | ||
+ | * [[https:// | ||
* [[https:// | * [[https:// | ||
Line 172: | Line 173: | ||
intentDataHandler.putExtra(" | intentDataHandler.putExtra(" | ||
intentDataHandler.putExtra(" | intentDataHandler.putExtra(" | ||
+ | intentDataHandler.putExtra(" | ||
startActivity(intentDataHandler); | startActivity(intentDataHandler); | ||
break; | break; | ||
Line 190: | Line 192: | ||
setContentView(R.layout.activity_target); | setContentView(R.layout.activity_target); | ||
| | ||
- | Bundle bundle = getIntent().getExtras(); | + | |
+ | // Get data payload (if available) | ||
+ | // | ||
+ | // Method 1 | ||
+ | | ||
String str = bundle.getString(" | String str = bundle.getString(" | ||
- | | ||
Toast.makeText(this, | Toast.makeText(this, | ||
| | ||
- | // Better yet | + | // Method 2 |
- | | + | |
- | str = getIntent().getExtras().getString(" | + | if (intent.hasExtra(" |
+ | str = intent.getExtras().getString(" | ||
Toast.makeText(this, | Toast.makeText(this, | ||
+ | } | ||
+ | |||
+ | // Method 3 | ||
+ | if (getIntent().hasExtra(" | ||
+ | str = getIntent().getExtras().getString(" | ||
+ | Toast.makeText(this, | ||
+ | } | ||
+ | |||
+ | // Parcelable data | ||
+ | Team objTeam; | ||
+ | if (intent.hasExtra(" | ||
+ | Log.d(" | ||
+ | objTeam = intent.getParcelableExtra(" | ||
+ | Toast.makeText(this, | ||
} | } | ||
} | } | ||
Line 205: | Line 224: | ||
</ | </ | ||
+ | A parcelable extra must be of a type supporting parcelable. | ||
+ | <code java> | ||
+ | package com.acme.myapp; | ||
+ | |||
+ | import android.os.Parcel; | ||
+ | import android.os.Parcelable; | ||
+ | |||
+ | public class Team implements Parcelable | ||
+ | { | ||
+ | public long id = 0; | ||
+ | public String name = ""; | ||
+ | public String number = ""; | ||
+ | public int ranking = 0; | ||
+ | |||
+ | // Default constructor | ||
+ | Team() | ||
+ | { | ||
+ | |||
+ | } | ||
+ | |||
+ | // In constructor, | ||
+ | // Important: Read them in the same sequence in which they were written in Parcel. | ||
+ | public Team(Parcel in) { | ||
+ | id = in.readLong(); | ||
+ | name = in.readString(); | ||
+ | number = in.readString(); | ||
+ | ranking = in.readInt(); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public int describeContents() { | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | // Write member variables in Parcel. | ||
+ | // Write in any order. Not required to write all members in Parcel. | ||
+ | @Override | ||
+ | public void writeToParcel(Parcel dest, int flags) { | ||
+ | // Write data in any order | ||
+ | dest.writeLong(id); | ||
+ | dest.writeString(name); | ||
+ | dest.writeString(number); | ||
+ | dest.writeInt(ranking); | ||
+ | } | ||
+ | |||
+ | // De-serialize the object | ||
+ | public static final Parcelable.Creator< | ||
+ | public Team createFromParcel(Parcel in) { | ||
+ | return new Team(in); | ||
+ | } | ||
+ | |||
+ | public Team[] newArray(int size) { | ||
+ | return new Team[size]; | ||
+ | } | ||
+ | }; | ||
+ | } | ||
+ | </ | ||
=== Intent Examples === | === Intent Examples === | ||
For example, in the '' | For example, in the '' | ||
Line 235: | Line 311: | ||
case R.id.btnDialNumber: | case R.id.btnDialNumber: | ||
Intent intentDialNumber = new Intent(Intent.ACTION_DIAL, | Intent intentDialNumber = new Intent(Intent.ACTION_DIAL, | ||
- | startActivity(intentDialNumber); | + | |
+ | | ||
+ | } | ||
break; | break; | ||
| | ||
case R.id.btnViewWebsite: | case R.id.btnViewWebsite: | ||
Intent intentViewWebsite = new Intent(Intent.ACTION_VIEW, | Intent intentViewWebsite = new Intent(Intent.ACTION_VIEW, | ||
- | startActivity(intentViewWebsite); | + | |
+ | | ||
+ | } | ||
break; | break; | ||
| | ||
Line 246: | Line 326: | ||
Intent intentShowLocation = new Intent(Intent.ACTION_VIEW, | Intent intentShowLocation = new Intent(Intent.ACTION_VIEW, | ||
Uri.parse(" | Uri.parse(" | ||
- | )); | + | )); |
- | startActivity(intentShowLocation); | + | |
+ | | ||
+ | } | ||
break; | break; | ||
| | ||
Line 253: | Line 335: | ||
// Display all activities supporting Action View that can handle intent | // Display all activities supporting Action View that can handle intent | ||
Intent intentShowAllActivities = new Intent(Intent.ACTION_VIEW); | Intent intentShowAllActivities = new Intent(Intent.ACTION_VIEW); | ||
- | startActivity(intentShowAllActivities); | + | |
+ | | ||
+ | } | ||
break; | break; | ||
| | ||
Line 260: | Line 344: | ||
SampleActivity.class | SampleActivity.class | ||
); | ); | ||
- | startActivity(intentShowToast); | + | |
+ | | ||
+ | } | ||
break; | break; | ||
| | ||
case R.id.btnShowSampleActivity: | case R.id.btnShowSampleActivity: | ||
Intent intentShowSampleActivity = new Intent(" | Intent intentShowSampleActivity = new Intent(" | ||
- | startActivity(intentShowSampleActivity); | + | |
+ | | ||
+ | } | ||
break; | break; | ||
} | } |