eng. wafaa audah - الصفحات...

16
1 Islamic University of Gaza Faculty of Engineering Computer Engineering Department Mobile Computing ECOM 5341 By Eng. Wafaa Audah July 2013

Upload: nguyennguyet

Post on 18-May-2018

219 views

Category:

Documents


1 download

TRANSCRIPT

1

Islamic University of Gaza

Faculty of Engineering

Computer Engineering Department

Mobile Computing ECOM 5341

By

Eng. Wafaa Audah

July 2013

2

Launch activitits, implicit intents, data

passing & start activity for result

Intents

An Android application could include any number of activities.

• Activities are independent of each other; however they usually cooperate exchanging

data and actions.

• Moving from one activity to another is accomplished by asking the current activity to

execute an intent.

Intents are used to start new activities, either explicitly or implicitly.

Intents are used as a message-passing mechanism within and between

applications.

Intents are used for broadcasting messages across the system.

Intents Types

There are two primary forms of intents you will use.

- Explicit Intents: provides the exact class to be run. Often these will not include any other information, simply being a way for an application to launch various internal activities it has as the user interacts with the application.

- Implicit Intents: they must include enough information for the system to determine which of the available components is best to run for that intent. Android provides some implicit actions that can be caught and handled by OS.

The main arguments of implicit Intent are:

- Action: The built-in action to be performed, such as ACTION_VIEW,

ACTION_MAIN,… etc.

- Data: The primary data to operate on, such as a phone number to be called

(expressed as tel: , http://, smsto:)

3

Launch activity:

Intent myActivity = new Intent (action, data);

startActivity (myActivity);

Examples of native action/data pairs are:

ACTION_DIAL tel:1234567 Display the phone dialer with the given number filled in.

ACTION_CALL tel:1234567 Brings up a phone dialer and immediately initiates a call using

the number supplied in the Intent URI.

ACTION_VIEW

- ACTION_VIEW http://www.google.com Show Google page in a browser view.

- ACTION_VIEW content://contacts/ people/ Display a list of people, which the

user can browse through. Selecting a particular person to view would result in a

new intent.

ACTION_EDIT content://contacts/people/2 Edit information about the contact

person whose identifier is "2".

ACTION_PICK Launches a sub-Activity that lets you pick an item from the Content Provider

specified by the Intent URI. When closed it should return a URI to the item that was picked.

ACTION_SENDTO smsto: 1234567 Launches an Activity to send a message to the contact

specified by the Intent URI.

Implicit Intents Examples

Intent i = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:+123456789"));

startActivity(i); Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));

startActivity(i);

4

Lab Work 1

Create an interface as the figure below shows.

When the user enter phone number and click Call button,

It calls the number in the EditText.

(This will need permission; permissions are clarified at the next page)

5

Permission

Permission is a restriction limiting access to a part of the code or to data on the device. The limitation is imposed to protect critical data and code that could be misused to distort or damage the user experience.

For example, here are some of the permissions defined by Android:

android.permission.CALL_EMERGENCY_NUMBERS android.permission.CALL_PHONE android.permission.READ_OWNER_DATA android.permission.SET_WALLPAPER android.permission.DEVICE_POWER android.permission.INTERNET

How to make it

6

<uses-permission> Requests a permission that the application must be granted in order for it to operate correctly. Permissions are granted by the user when the application is installed, not while it's running.

<permission> Declares a security permission that can be used to limit access to specific components or features of this or other applications.

7

Intents used to:

Launch an Activity (explicitly)

Intent i = new Intent (CurrentActivity.this, DestinationActivity.class);

startActivity(i);

(All activities must be added to the AndroidManifest.xml File)

- Data-passing mechanism between activities (explicitly)

To share primitive data between Activities/Services in an application, use

Intent.putExtra (key,value), getExtra(key) is used to extract data from the intent.

First Activity

Second Activity

putExrta can take String, Integer, Double, Boolean, IntArray and other types.

Extra ….. Using Bundle

- The Android Bundle container is a simple mechanism used to pass data between

co-operating activities. It is a type-safe MAP collection of <key, value> pairs.

- There is a set of putXXX and getXXX methods to store and retrieve (single and

array) values of primitive data types from/to the bundles.

Intent i = new Intent(First.this,Second.class); i.putExtra("msg", "hi"); startActivity(i);

Intent i = this.getIntent(); String x = i.getStringExtra("msg");

8

- The code using an additional bundle is slightly heavier(it won't make any

difference in any practical application) and slightly easier to manage, being more

general(If you decide that - before sending information inside an intent - you

want to serialize the data to database - it will be a bit cleaner to have a bundle

that you can serialize, add to intent and then feed to a Bundle all in one object)

Example

First Activity

Second Activity

Note: In order to get the array at a correct shape :

String array2Str=" "

for (int m=0; m<code.length; m++) { array2Str = array2Str + "," + Integer.toString( code[m] ); }

Intent i= new Intent(First.this, Second.class);

Bundle extras = new Bundle();

extras.putString("Username","name");

extras.putInt("Password","pass");

int[] code={1,2,3};

extras.putIntArray("Code",code);

extras.putBoolean("Check",true);

intent.putExtras(extras);

startActivity(intent);

Bundle extras = getIntent().getExtras();

String name = extras.getString("Username ");

int password = extras.getInt("Password");

int [] code = extras.getIntArray("Code");

boolean check = extras.getBoolean("Check");

9

Create an interface as the figure below shows.

- User fills the two fields and checks the checkbox, then click Save in the

first activity:

- The second activity must get the entered data in first activity and its

interface appears by this way:

Your Name is … (from first activity)

Your ID is … ((from first activity))

Accept rules checkbox status is … (from first activity)

(Using textView/s)

Hint: At first: i.putExtra("ID", 22446688);

i.putExtra("accept", true); At second: int y = i.getIntExtra("ID",-1);

<< -1 : default value if null is returned >> boolean z = i.getBooleanExtra("accept",false);

<< false : Default value >>

Lab Work 2

10

Starting an Activity for a result

- In order to get results back from the called activity we use the method

startActivityForResult ( Intent, requestCodeID )

- Where requestCodeID is an arbitrary value you choose to identify the call.

(similar to a ‘nickname’ ).

- The result sent by the sub-activity could be picked up through the listener-like

asynchronous method

onActivityResult ( requestCodeID, resultCode, Intent )

- Before an invoked activity exits, it can call setResult(resultCode) to return a

termination signal back to its parent.

FirstActivity

final public static int SECOND_ACTIVITY_ID= 2; Intent i= new Intent (FirstActivity.this, SecondActivity.class) startActivityForResult(i, SECOND_ACTIVITY_ID); @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case SECOND_ACTIVITY_ID: if(resultCode==Activity.RESULT_OK){ String name = data.getStringExtra("Username"); }else if (resultCode==Activity.RESULT_CANCELED){ //Do Something } break;

// if there are more one activity that we want to “startActivityForResult” // for it, many cases will be added here with different activity numbers give above

}

11

SecondActivity

Intent i = new Intent(); data.putExtra("Username","myName"); setResult(RESULT_OK, i); finish();

Go back to lab work 2, add button and textview/s as the figure

below shows.

When user click More the following interface will appear, user fill

the fields, clicks Save, entered data will be returned back to the

main interface at the textview

Lab Work 3

12

Homework

Create an activity << FirstOne >>interface that contains 2buttons

<<Review>> & << Let’s Go >> and textView/s for << Result >>

Create another three activities (classes) at the same application

- 1st: MainTabActivity

- 2nd: MessageTab : its interface contains: 1. editText to enter phone

number that you will send msg for it, 2. button << view >>

- 3rd: WebTab : its interface contains: 1. editText to enter the

website link that you will want to view, 2. button < finish >> 3.

button << view >>

- Two activities (MessageTab and WebTab) will be Tabbed.

(Every tab represents activity, MainTabActivity will be the main

container activity that contain the tabhost)

Scenario will be as the following:

FirstOne interface appear …. When user clicks Let’s Go:

1. MainTabActivity will be started for result, it will show

MessageTab, WebTab in tabs, user will work with both of

activities.(send msg, view web :on “view” button click)

2. MainTabActivity will return the entered data from them *** (on

finish button click), and it will return these result to FirstOne .

3. Result will appear at FirstOne on textViews.

When user clicks Review, interface at labwork2 will appear,

entered data will be returned at the textview at FirstOne:

*** Msg sent successfully to: (from returned result 1)

The following website opened successfully: (from returned result 1)

Your Name is: (from returned result 2)

Your ID is: (from returned result 2)

Accept rules checkbox status is: (from returned result 2)

13

Tabs

Sometimes, we want to wrap multiple views in a single window and navigate through them with a Tab Container. This can be done in Android using TabHost control.

There are two ways to use a TabHost application in Android:

1. Using the TabHost to navigate through multiple views within the same activity. 2. Using the TabHost to navigate through Actual multiple Activities using intents

(wanted!)

Anatomy of Tabbed Application

An activity with a TabHost may look like this:

The Activity consists of:

1. A TabHost: The root element of the layout 2. The TabHost wraps a TabWidget which represents the tab bar. 3. The TabHost wraps a FrameLayout which wraps the contents of each tab.

14

- What if we have multiple Activities in our application and we want to navigate between them using tabs; In this case, we will have one activity as the root activity of the application.

- This activity will have the TabHost and will navigate to other activities using Intents.

Note: The root activity must inherit from TabActivity.

The root activity will have layout file like this:

<?xml version="1.0" encoding="utf-8"?>

<TabHost android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@android:id/tabhost"

xmlns:android="http://schemas.android.com/apk/res/android"

>

<TabWidget

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@android:id/tabs"

/>

<FrameLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@android:id/tabcontent"

>

</FrameLayout>

</TabHost>

15

In order to view two tabs, the foloowing code is used

1. We create tabs using TabSpecs class. 2. We set the title of each tab using TabSpecs.setIndicator() method. 3. We set the content of each tab using TabSpecs.setContent() method.

import android.os.Bundle; import android.app.Activity; import android.app.TabActivity; import android.content.Intent; import android.widget.TabHost; public class TaActivity extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab); TabHost tabHost=getTabHost(); TabHost.TabSpec spec; Intent i1=new Intent(TaActivity.this,one.class); spec=tabHost.newTabSpec("tab1").setIndicator("Tab1").setContent(i1); tabHost.addTab(spec); Intent i2=new Intent(TaActivity.this,two.class); spec=tabHost.newTabSpec("tab2").setIndicator("Tab2").setContent(i2); tabHost.addTab(spec); tabHost.setCurrentTab(0);

}}

16

Important note ……. At your homework:

1. MainTabActivity interface will be typically like the xml file above. 2. Code will be written within MainTabActivity. 3. Tabs will have interfaces as specified before. 4. Change application theme into Theme.Black.NoTitleBar. 5. You can choose an icon photo for every tab (optional).

In order to see an example of how to set the contents of tabs by specifying multiple layout resources to be displayed within the same activity, visit the following link (optional)

http://www.codeproject.com/Articles/107693/Tabbed-Applications-in-Android

SEE YOU AT THE NEXT LAB