workshop - lo chi wing's personal web siteandroid apps development for mobile and tablet device...

20
Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent (Page 1 – 2) Launch a Camera for Photo Taking 2. Create Intent with Parsing Data (Page 3 – 8) Making Phone Call and Dial Access Web Content Playing YouTube Video 3. Create Intent with Extra (Page 9 – 11) Sending SMS Perform a Web Search 4. Create Intent with Action and Type (Page 12 – 13) Sending Email 5. Create Intent with Result return (Page 14 – 17) Voice Recognition 6. Create Master Detail Flow Fragment using system template (Page 18 – 19)

Upload: others

Post on 10-Mar-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015

Workshop

1. Create a simple Intent (Page 1 – 2)

․ Launch a Camera for Photo Taking

2. Create Intent with Parsing Data (Page 3 – 8)

․ Making Phone Call and Dial

․ Access Web Content

․ Playing YouTube Video

3. Create Intent with Extra (Page 9 – 11)

․ Sending SMS

․ Perform a Web Search

4. Create Intent with Action and Type (Page 12 – 13)

․ Sending Email

5. Create Intent with Result return (Page 14 – 17)

․ Voice Recognition

6. Create Master Detail Flow Fragment using system template (Page 18 – 19)

Page 2: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 1

1. Simple Intent

1.1 Launch a Camera for Photo Taking 1. Create the Android application with the following attributes.

․ Application Name: MyPhotoIntent

․ Project Name: MyPhotoIntent

․ Package Name: com.example.myphotointent

2. Modify the source file "MainActivity.java" as follow: package com.example.myphotointent;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.content.Intent;

import android.provider.MediaStore;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Create the intent for launching the camera for photo taking

Intent myIntent = new Intent(MediaStore.ACTION_IM AGE_CAPTURE);

// Start the Intent activity

startActivity(myIntent);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

Page 3: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 2

3. Open the Android Manifest file, and press [Add] in the “Permission” tab. Then select “Uses

Permission” and press [OK].

4. Select the permission “android.permission.CAMERA” and save.

5. Save and execute the app, you should able to launch the camera for photo taking (This app does

not work properly in emulator).

Page 4: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 3

2. Intent with Parse Data

2.1 Make Phone Call 1. Create the Android application with the following attributes.

․ Application Name: MyDialIntent

․ Project Name: MyDialIntent

․ Package Name: com.example.mydialintent

2. Modify the source file "MainActivity.java" as follow: package com.example.mydialintent;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.content.Intent;

import android.net.Uri;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Define the data: phone number

String myData = "tel:27665111";

// Create the intent for display the phone dialer w ith the given number

Intent myIntent = new Intent(Intent.ACTION_DIAL, Ur i.parse(myData));

// Start the Intent activity

startActivity(myIntent);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

Page 5: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 4

}

}

3. Save and execute the app, you should able to see the dial up screen with the telephone number.

4. Modify the source file "MainActivity.java" by changing word from “ACTION_DIAL” to

“ACTION_CALL”.

5. Open the Android Manifest file, and press [Add] in the “Permission” tab. Then select “Uses

Permission” and press [OK].

Page 6: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 5

6. Select the permission “android.permission.CALL_PHONE” and save.

7. Execute the app again, you mobile will make a phone call to the number you input immediately.

2.2 Play YouTube Video 1. Create the Android application with the following attributes.

․ Application Name: MyYouTube

․ Project Name: MyYouTube

․ Package Name: com.example.myyoutube

2. Modify the source file "MainActivity.java" as follow: package com.example.myyoutube;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.content.Intent;

import android.net.Uri;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Page 7: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 6

// Define the variables

Uri uri = Uri.parse("https://www.youtube.com/watc h?v=fy_3a5gFNJA");

uri = Uri.parse("vnd.youtube:" + uri.getQueryPara meter("v"));

// Create the intent for viewing YouTube

Intent intent = new Intent(Intent.ACTION_VIEW, ur i);

// Start the Intent activity

startActivity(intent);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

3. Open the manifest file “AndroidManifest.xml”, and press [Add] in the “Permission” tab. Then

select “Uses Permission” and press [OK].

Page 8: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 7

4. Select the permission “android.permission.INTERNET” and save.

5. Save and execute the app, the YouTube video will be shown.

2.3 Access Web Content 1. Create the Android application with the following attributes.

․ Application Name: MyWebIntent

․ Project Name: MyWebIntent

․ Package Name: com.example.mywebintent

2. Modify the source file "MainActivity.java" as follow: package com.example.mywebintent;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.content.Intent;

import android.net.Uri;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Page 9: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 8

// Define the data: Web URL

String myData = "http://www.polyu.edu.hk";

// Create the intent for starting the browser

Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myData));

// Start the Intent activity

startActivity(myIntent);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

3. Save and execute the app, you should able to see the PolyU website

Page 10: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 9

3. Intent with Extra

3.1 Sending SMS 1. Create the Android application with the following attributes.

․ Application Name: MySMSIntent

․ Project Name: MySMSIntent

․ Package Name: com.example.mysmsintent

2. Modify the source file "MainActivity.java" as follow: package com.example.mysmsintent;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.content.Intent;

import android.net.Uri;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Define the data: phone number

String myData = "smsto:27665111";

// Create the intent for sending SMS

Intent myIntent = new Intent(Intent.ACTION_SENDTO , Uri.parse(myData));

// The text is supplied as an Extra element called “sms_body”.

myIntent.putExtra("sms_body", "Testing Message");

// Start the Intent activity

startActivity(myIntent);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

Page 11: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 10

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

3. Save and execute the app, you should able to see the SMS.

3.2 Perform a Web Search 1. Create the Android application with the following attributes.

․ Application Name: MySearchIntent

․ Project Name: MySearchIntent

․ Package Name: com.example.mysearchintent

2. Modify the source file "MainActivity.java" as follow: package com.example.mysearchintent;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.content.Intent;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Create the intent for call web search

Page 12: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 11

Intent myIntent = new Intent(Intent.ACTION_WEB_SE ARCH);

// Put the word “PolyU” to the intent for query

myIntent.putExtra(SearchManager.QUERY, "PolyU");

// Start the Intent activity

startActivity(myIntent);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

3. Save and execute the app, can you see the result for the searching?

Page 13: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 12

4. Intent with Action and Type

4.1 Sending Email 1. Create the Android application with the following attributes.

․ Application Name: MyEmailIntent

․ Project Name: MyEmailIntent

․ Package Name: com.example.myemailintent

2. Modify the source file "MainActivity.java" as follow: package com.example.myemailintent;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.content.Intent;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Define the variables for sending out email

String myEmailSubject = "Android Course";

String myEmailText = "Testing Email from your mob ile";

String emailReceiverList[] = {"[email protected] du.hk"};

// Create the intent for sending out email

Intent myIntent = new Intent(Intent.ACTION_SEND);

// Define the MIME type as plain text

myIntent.setType("text/plain");

// Assign the receiver, subject and email content to extra

myIntent.putExtra(Intent.EXTRA_EMAIL, emailReceiv erList);

myIntent.putExtra(Intent.EXTRA_SUBJECT, myEmailSu bject);

myIntent.putExtra(Intent.EXTRA_TEXT, myEmailText) ;

Page 14: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 13

// Start the Intent activity

startActivity(myIntent);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

3. Save and execute the app, you should able to create the email:

Page 15: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 14

5. Intent with Result Return

5.1 Voice Recognition 1. Create the Android application with the following attributes.

․ Application Name: MyVoiceRecognition

․ Project Name: MyVoiceRecognition

․ Package Name: com.example.myvoicerecognition

2. Remove the default text view “Hello World”

3. Right click the layout, and select Change Layout.

4. Change the layout to List View.

Page 16: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 15

5. Modify the source file "MainActivity.java" as follow: package com.example.myvoicerecognition;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.content.Intent;

import android.speech.RecognizerIntent;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends Activity {

private static final int REQUEST_CODE = 1234;

private ListView resultList;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

resultList = (ListView) findViewById(R.id.ListView1 );

// Create the intent for voice recognition

Intent myIntent = new Intent(RecognizerIntent.ACT ION_RECOGNIZE_SPEECH);

// Assign the corresponding parameter

myIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE _MODEL,

RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

myIntent.putExtra(RecognizerIntent.EXTRA_PROMPT,

"AndroidBite Voice Recognition...");

// Start the Intent activity

startActivityForResult(myIntent, REQUEST_CODE);

}

@Override

protected void onActivityResult(int requestCode, in t resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, d ata);

Page 17: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 16

if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {

// Return the result to the list

ArrayList<String> matches =

data.getStringArrayListExtra(RecognizerIntent.E XTRA_RESULTS);

resultList.setAdapter(new ArrayAdapter<String>(t his,

android.R.layout.simple_list_item_1, matches));

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

6. Open the Android Manifest file, and press [Add] in the “Permission” tab. Then select “Uses

Permission” and press [OK].

7. Select the permission “android.permission.INTERNET” and save.

Page 18: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 17

8. Save and execute the app, then speech something to your mobile. (This app does not work

properly in emulator).

Page 19: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 18

6. Fragment Template

6.1 Master Detail Flow 1. Create the Android application with the following attributes.

․ Application Name: MyFirstFragment

․ Project Name: MyFirstFragment

․ Package Name: com.example.myfirstfragment

Page 20: Workshop - Lo Chi Wing's Personal Web SiteAndroid Apps Development for Mobile and Tablet Device (Level I) Lesson 5 X4-XT-CDP-0251-A @ Peter Lo 2015 Workshop 1. Create a simple Intent

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 5

X4-XT-CDP-0251-A @ Peter Lo 2015 19

2. The template will generate automatically, you can customize the fragment in the onCreateView()

method of “ItemDetailFragment.java”.

3. Execute the app using a mobile phone AVD.

4. Execute the app using a tablet AVD.