Android App Development 07 : Intent & Share
-
Upload
anuchit-chalothorn -
Category
Documents
-
view
476 -
download
4
Transcript of Android App Development 07 : Intent & Share
![Page 1: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/1.jpg)
Intent & ShareAnuchit [email protected]
7Anuchit [email protected]
Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
![Page 2: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/2.jpg)
Intent
Intents are asynchronous messages which allow Android components to request functionality from other components of the Android system.
Ref: http://developer.android.com/reference/android/content/Intent.html
![Page 3: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/3.jpg)
Intent to another activity
Intent to the Android system to starts another activity
Intent intent = new Intent(this,NewActivity.class);startActivity(intent);
![Page 4: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/4.jpg)
Intent with data
An Intent can contain data. This data can be used by the receiving component. As data it may send the URL to the browser component which this browser should open and display.
String url="http://google.com";Intent intent = new Intent(Intent.ACTION_VIEW);intent.sendData(Uri.parse(url));startActivity(intent);
![Page 5: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/5.jpg)
Explicit Intents
Explicit intents explicitly defines the component which should be called by the Android system, by using the Java class as identifier.
Intent i = new Intent(this,NewActivity.class);i.putExtra("Value1", "This value one for ActivityTwo ");i.putExtra("Value2", "This value two ActivityTwo"); startActivity(i);
![Page 6: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/6.jpg)
Implicit Intents
Implicit intents specify the action which should be performed and optionally data which provides data for the action.
Intent intent = new Intent(Intent.ACTION_VIEW);intent.sendData(Uri.parse("http://google.com"));startActivity(intent);
![Page 7: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/7.jpg)
Using the share intent
Lots of Android applications allow you to share some data with other people, e.g. the Facebook, G+, Gmail and Twitter application. You can send data to one of this components.
![Page 8: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/8.jpg)
Share Intent Example
You can send data to one of this components.
Intent intent = new Intent(Intent.ACTION_SEND);intent.setType("text/plain");intent.putExtra(android.content.Intent.EXTRA_TEXT, "News for you!");startActivity(intent);
![Page 9: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/9.jpg)
Receive Intent data
The component which receives the Intent can use the getIntent().getExtras() method call to get the extra data.
Bundle extras = getIntent().getExtras();if (extras != null) {
String value1 = extras.getString(Intent.EXTRA_TEXT);}
![Page 10: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/10.jpg)
Intent Filters
If an Intents is send to the Android system, it will determine suitable applications for this Intents. If several components have been registered for this type of Intents, Android offers the user the choice to open one of them.
![Page 11: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/11.jpg)
Intent Filters
An Intent Filter declares the capabilities of a component. It specifies what an activity or service can do and what types of broadcasts a Receiver can handle. It allows the corresponding component to receive Intents of the declared type. IntentFilters are typically defined via the AndroidManifest.xml file
![Page 12: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/12.jpg)
Register your Activity as Browser
Register an Activity for the Intent which is triggered when someone wants to open a webpage
<activity android:name=".BrowserActivitiy" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http"/> </intent-filter></activity>
![Page 13: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/13.jpg)
Register your Activity for the Share Intent
Register an Activity for the ACTION_SEND Intent for the text/plain mime type.<activity
android:name=".ActivityTest"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
![Page 14: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/14.jpg)
Workshop: Explicit Intents
Create 2 Activities, first activity has a edit text and button after push the button it 'll sent data in edit text to the second activity.
![Page 15: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/15.jpg)
![Page 16: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/16.jpg)
Workshop: Implicit Intents
Create an app to use a Spinner to select the Intent which should get trigger :
● Open Browser○ Intent.ACTION_VIEW○ Uri.parse("http://google.com");
● Call Someone○ Intent.ACTION_CALL○ Uri.parse("tel:0891234567");
● Dial○ Intent.ACTION_DIAL○ Uri.parse("tel:0891234567");
![Page 17: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/17.jpg)
Workshop: Implicit Intents
● Show Map○ Intent.ACTION_VIEW○ Uri.parse("geo:0,0?q=100.00,15.00");
● Search on Map○ Intent.ACTION_VIEW○ Uri.parse("geo:0,0?q=Bluecup coffee");
![Page 18: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/18.jpg)
![Page 19: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/19.jpg)
Workshop: Share Intent
Create App with simple share, to share text in edit text to another application.
Intent intent = new Intent(Intent.ACTION_SEND);intent.setType("text/plain");intent.putExtra(Intent.EXTRA_TEXT, shareText);startActivity(intent);
![Page 20: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/20.jpg)
![Page 21: Android App Development 07 : Intent & Share](https://reader033.fdocuments.us/reader033/viewer/2022052523/55557b4bb4c9055f5f8b4ee5/html5/thumbnails/21.jpg)
End