android basic components

85
Android Basic Components Jussi Pohjolainen

Upload: jussi-pohjolainen

Post on 19-May-2015

13.924 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Android Basic Components

Android  Basic  Components

Jussi  Pohjolainen

Page 2: Android Basic Components

App  Fundamentals

• Android  app  lives  in  its  own  world– Own  process,  app  files  only  visible  to  the  app

• Apps  can  make  use  of  other  apps,  information  sharing,  moving  between  apps

• Apps  are  build  using  Android  components• Every  app  holds  a  application  description  file  (AndroidManifest.xml)– In  the  file  you  define  the  android  components

Page 3: Android Basic Components

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="fi.company.project.exampleproject" >

<applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activity

android:name=".MainActivity"android:label="@string/app_name" ><intent-filter>

<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />

</intent-filter></activity>

</application>

</manifest>

Page 4: Android Basic Components

Components

• Android  app  is  built  using  components– Activity:  User  visible  Window– Service:  Background  service  (no  UI)– Broadcast  Receiver:  receiving  broadcasts  from  apps  and  system  services

– Content  provider:  Provides  content  to  apps• Components  are  separate  building  blocks  that  can  be  accessed  by  other  apps!

• Components  are  usually  declared  in  application  manifest

Page 5: Android Basic Components

ACTIVITIES  AND  INTENTS

Page 6: Android Basic Components

1.  Activities

• An  activity  is  a  single,  focused  thing  that  the  user  can  do  – Equivalent  to  Frame  or  Window  in  GUI  toolkits

• Subclass  of  Activity – class• One  app  may  have  one  or  several  activities• Each  activity  is  given  a  default  window  to  draw  in

• Window  consists  of  views  (widgets)

Page 7: Android Basic Components

Some  Activities  from  Android  2.x

Page 8: Android Basic Components

Activity  Stack

• Android  keeps  navigation  history  of  activities  the  user  has  visited:  Activity  Stack  or  the  Back  Stack

• Pressing  Back  displays  the  previous  Activity!• User  cannot  go  further  than  the  last  visit  of  home

Page 9: Android Basic Components

Back  Stack

Page 10: Android Basic Components

About  Tasks

• Task  is  a  sequence  of  activities• Task  can  hold  activities  from  several  apps• Activity  that  starts  the  task  is  called  root  activity– Usually  started  from  home  screen

• New  task  is  started  when  new  app  is  launched.  Also  new  task  can  be  started  on  certain  activities  (opening  browser,  maps..)

• Recent  task  switcher  shows  the  recent  tasks..

Page 11: Android Basic Components

Tasks

Page 12: Android Basic Components

Activity  and  Tasks

Page 13: Android Basic Components

Reusing  Activities

Page 14: Android Basic Components

Activity  Lifecycle

Page 15: Android Basic Components

Activity  Lifecycle:  States

• Resumed– App  is  in  foreground  and  user  can  interact  with  it

• Paused– Partially  obscure  by  another  activity.  

• Stopped– not  visible,  in  background.  All  member  variables  are  retained,  cannot  execute  code.  

• Other  states– Createdand  Started  – system  quickly  moves  from  them  to  the  next  state!

Page 16: Android Basic Components

public class Activity extends ApplicationContext {

protected void onCreate(Bundle savedInstanceState);

protected void onStart();

protected void onRestart();

protected void onResume();

protected void onPause();

protected void onStop();

protected void onDestroy();}

// The derived class must invoke these: super.onCreate(…), // super.onStart()..

Page 17: Android Basic Components

• onCreate()– Create  the  user  interface

• onStart()– When  visible  to  user

• onResume()– Activity  is  visible  again,  initialize  fields,  register  listeners,  bind  to  services

• onPause()– Activity  still  partially  visible,  but  most  often  is  an  indication  that  the  user  is  leaving  the  activity  and  it  will  soon  enter  the  stopped  state.  Release  resources,  save  app  data,  unregister  listeners,  unbind  services

• onStop()– Activity  is  no  longer  visible  to  user.  Time  or  CPU  intensive  shut-­‐down  operations   like  writing  information  to  database

Page 18: Android Basic Components

"Importance  Hierarchy"

• When  running  out  of  memory,  what  to  kill?1. Background  Process  – When  task  is  not  visible  to  

user2. Service  Process  – killed  only  if  memory  is  

needed  for  foreground  and  visible  processes3. Visible  Process  – killed  only  if  keep  foreground  

processes  alive4. Foreground  Process  – killed  only  as  last  resort

Page 19: Android Basic Components

// ACTIVITY 1

public class Activity1 extends Activity implements OnClickListener {

private Button changeActivity;

@Overridepublic void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.main1);

changeActivity = (Button) findViewById(R.id.Button01);changeActivity.setOnClickListener(this);

}

@Overridepublic void onClick(View v) {

Intent intent = new Intent(this, Activity2.class);startActivity(intent);

}}

Page 20: Android Basic Components

// AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="fi.tamk”>

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".Activity1"android:label="@string/activity1">

<intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activity android:name=".Activity2"android:label="@string/activity2" />

</application></manifest>

Page 21: Android Basic Components

State  Information

• Think  about  following  situation:1. User  opens  app  and  Activity  1  opens.  User  is  

prompt  a  name  in  EditText.  User  writes  his/her  name.

2. User  navigates  from  Activity  1  to  Activity  23. User  presses  back  button  and  Activity  1  is  

opened  again• Is  the  name  still  there?

Page 22: Android Basic Components

How  to  Store  State  Information

• Store  state:– onSaveInstanceState(Bundle)

• Read  state– onRestoreInstanceState(Bundle)

• This  will  store  data  only  temporarily:  for  app  lifetime!

• Data  will  be  held  in  memory  until  the  app  is  closed!

Page 23: Android Basic Components

Store@Overridepublic void onSaveInstanceState(Bundle savedInstanceState) {

String text = textfield.getText().toString();savedInstanceState.putString("someKey", text);

super.onSaveInstanceState(savedInstanceState);}

Page 24: Android Basic Components

Load@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) {super.onRestoreInstanceState(savedInstanceState);

if (savedInstanceState != null) {String strValue = savedInstanceState.getString("someKey");

if (strValue != null){

textfield.setText(strValue);}

}}

Page 25: Android Basic Components

INTENTS

Page 26: Android Basic Components

Intro  to  Intents

• Intents are  message-­‐passing  mechanism  that  lets  you  declare  your  intentation than  an  action  be  performed  with  a  particular  piece  of  data

• Interaction  between  any  android  component• Uses– Start  new  activities– Broadcast  messages– Start  new  services

Page 27: Android Basic Components

Intents  and  Intents  Filter

• Intents  can  be  used  to– start  activities  and  services– broadcast  data  between  components

• Intent– Message  to  someone– Request  an  action  to  be  performed– Interaction  between  any app  component  on  Android

• Intent  Filter  – Who  can  handle  the  message?– Register  Activity,  Service  and  Broadcast  receiver

Page 28: Android Basic Components

Explicit  vs Implicit  Intents

• Explicit– Open  explicitly  certain  Activity– Internal  messaging  between  your  app– Designated  target  class

• Implicit– External  messaging  between  apps– Open  some  Activity  with  certain  service  

• you  don’t  know  which  activity  of  which  app…

Page 29: Android Basic Components

Explicit  Intent// This is done in some Activity-class:Intent intent = new Intent(this,

SecondActivity.class);startActivity(intent)

Page 30: Android Basic Components

Sending  DataIntent intent = new Intent(this,

SecondActivity.class);

// Extra data is key/value pairs// Send dataintent.putExtra(“key”, “value”);startActivity(intent);

Page 31: Android Basic Components

Receiving  the  Informationpublic class SecondActivity extends Activity {

@Overridepublic void onCreate(Bundle bundle) {

super.onCreate(bundle);setContentView(R.layout.main);Bundle extras = getIntent().getExtras();if (extras != null){String value1 = extras.getString("key");

}....

Page 32: Android Basic Components

public class GettingResultsBack extends Activity {

private static final int REQUEST_CODE = 10;

...

public void clickButton(View v) {if (v == settings) {

Intent intent = new Intent(this,Settings.class);

startActivityForResult(intent, REQUEST_CODE);}

}

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

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

Bundle bundle = data.getExtras();String result = bundle.getString("somevalue");

}}

}}

Page 33: Android Basic Components

Getting  results  Back@Overridepublic void onBackPressed() {

Intent intent = new Intent();

number = phoneNumber.getText().toString();intent.putExtra("phonenumber", number);setResult(RESULT_OK, intent);

super.onBackPressed();}

Page 34: Android Basic Components

Implicit  Intents

• Implicit  Intents  are  mechanism  that  lets  open  anonymous  application’s  components

• Android  will  at  run  time  resolve  the  best  class  suited  to  performing  the  action– Your  app  will  use  other  app’s  functionality  without  knowing  exactly  which  application!

• Various  native  apps  provide  components  that  can  be  called  implicitly

Page 35: Android Basic Components

Implicit  Intent’s  Pieces

• Primary  pieces  of  information  in  Intent’s  are1. Action

• The  general  action  to  be  performed,   for  example  ACTION_DIAL, ACTION_VIEW

2. Data• The  data  to  operate  on  expressed  in  Uri

• Example  action/data  pairs– ACTION_VIEW, content://contacts/people/1– ACTION_VIEW, content://contacts/people/– ACTION_DIAL, tel://123456

• I  want  to  view  (action)  a  webpage  (URI)

Page 36: Android Basic Components

Using  Implicit  Intents

Intent intent = new Intent(Intent.ACTION_DIAL,

Uri.parse("tel:123456"));

startActivity(intent);

Intent intent = new Intent(Intent.ACTION_VIEW,

Uri.parse("http://..");

startActivity(intent);

Intent  Filter's  name

Data

Page 37: Android Basic Components

Lot  of  Native  Android  Actions• All  the  String  constants  can  be  found  from  Intent  class.  These  are  native  

actions.• Activity

– ACTION_ANSWER    (=“android.intent.action.ANSWER”)– ACTION_CALL– ACTION_BUG_REPORT– ACTION_POWER_USAGE_SUMMARY– …

• Broadcast– ACTION_BATTERY_CHANGED– ACTION_BATTERY_LOW– ACTION_BOOT_COMPLETED– ACTION_AIRPLANE_MODE_CHANGED– ACTION_CAMERA_BUTTON– ...

Page 38: Android Basic Components

public void clicked(View button) {...case R.id.button1:

intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.tamk.fi"));

startActivity(intent);break;

case R.id.button2:intent = new Intent(Intent.ACTION_CALL,

Uri.parse("tel:(+358)12345789"));startActivity(intent);break;

case R.id.button3:intent = new Intent(Intent.ACTION_DIAL,

Uri.parse("tel:(+358)12345789"));startActivity(intent);break;

case R.id.button4:intent = new Intent(Intent.ACTION_VIEW,

Uri.parse("geo:60.123,60.1434?z=19"));startActivity(intent);break;

case R.id.button5:intent = new Intent(Intent.ACTION_VIEW,

Uri.parse("geo:0,0?q=kauppakadun rauta"));startActivity(intent);break;

case R.id.button6:intent = new Intent("android.media.action.IMAGE_CAPTURE");startActivityForResult(intent, 0);break;

case R.id.button7:intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/"));startActivity(intent);break;

case R.id.button8:intent = new Intent(Intent.ACTION_EDIT, Uri.parse("content://contacts/people/1"));startActivity(intent);break;

}

Page 39: Android Basic Components

Intent  Filters

• How  does  Android  know  which  application  (and  component)  handles  the  request?

• Intent  Filters  are  used  to  register  components  as  being  capable  of  performing  an  action  on  particular  kind  of  data– Tell  Android  that  your  app  can  service  request  from  other  apps

• How?  Use  application’s  manifest  file  and  add  inter-filter tag

Page 40: Android Basic Components

Using  Intent  Filter

• Intent  action– Name  of  the  action  being  serviced.  Should  be  unique,  so  use  Java  package  naming  conventions

• Intent  type  /  data– type  of  data  given  to  component  (MIME  type)

• Intent  category– Additional  info  about  the  component  that  should  handle  the  action.  CATEGORY_BROWSABLE,  CATEGORY_PREFERENCES...

Page 41: Android Basic Components

Implementing  Own  Intent  Filter<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="fi.organization.demos”><uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".PlaySound"

android:label="@string/app_name"><intent-filter>

<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />

</intent-filter><intent-filter>

<action android:name="fi.organization.demos.PLAYSOUND" /><data android:scheme="http" /><category android:name="android.intent.category.DEFAULT" />

</intent-filter></activity>

</application></manifest>

Page 42: Android Basic Components

Opening  the  Activity  (App  A)intent =

new Intent("fi.organization.demos.PLAYSOUND",

Uri.parse("http://www….fi/music.mp3"));

startActivity(intent);

Page 43: Android Basic Components

The  Activity  (App  B)public class PlaySound extends Activity {

/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.main);

Intent i = getIntent();

Uri uri = i.getData();

if(uri != null) {MediaPlayer mp = MediaPlayer.create(this, i.getData());mp.start();

}}

}

Page 44: Android Basic Components

Implementing  a  Browser<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="fi.myorganization.mybrowser">

<application android:icon="@drawable/icon"android:label="@string/app_name">

<activity android:name=".MyBrowser"android:label="@string/app_name">

<intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />

</intent-filter><intent-filter>

<action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT" /><data android:scheme="http"/>

</intent-filter></activity>

</application>

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

</manifest>

Page 45: Android Basic Components

Application  Components

1. Activities  and  Intents2. Services3. Broadcast  receivers4. Content  provides

Page 46: Android Basic Components

2.  Services

• A  facility  for  the  application  to  tell  the  system  about  something  it  wants  to  be  doing  in  the  background– background  music– fetching  data  over  network.– file  i/o– ..

• Activity  may  be  frozen  when  user  moves  to  another  Activity.  Service  can  go  on  in  the  background.

Page 47: Android Basic Components

About  Services

• Service  is  NOT  a  separate  process  or  thread• Provides  two  features– Tell  the  system,  that  we  want  to  do  something  in  the  background.  (startService())  • Even  if  the  app  closes!

– The  ability  to  expose  functionality  to  other  apps  (bindService())

• Service  is  a  simple  class,  you  must  implement  separate  threads  by  yourself.

• Modify  the  Manifest  – file!

Page 48: Android Basic Components

Started  and  Bounded  Service• Started  Service

– Service   is  started  when  an  app  component  calls  startService()– Service  can  run  in  background  forever.  Usually  started  service  

performs  a  single  operation  and  does  not  return  to  caller– When  operation  is  done,  the  service  should  stop  itself

• Bounded   Service– Service   is  bounded  when  app  component  binds  to  it  by  calling  

bindService()– Bound  service  may  interact  with  the  service   (not  just  start  and  stop).– Bound  Service  is  run  only  as  long  as  app  is  bound  to  the  service

• Service  can  be  both,  run  indefinately and  allow  bounding

Page 49: Android Basic Components

public class MyService extends Service {/*

If client is binded to service, this method iscalled. Your implementation must return an objectthat implements IBinder interface. If you don't needthis, just return null.

*/@Overridepublic IBinder onBind(Intent arg0) {

return null;}

@Overridepublic void onDestroy() {

}/*

When Activity calls startService(..), thismethod is invoked. Service is started and it'srunning until stopService or stopSelf() is called

*/@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {

return START_STICKY;}@Overridepublic void onCreate() {

}}

Page 50: Android Basic Components

public class MainActivity extends Activity implements OnClickListener{

@Overridepublic void onClick(View v) {

if (v == startService) {Intent intent = new Intent(this, MyService.class);startService(intent);

} else if (v == stopService) {Intent intent = new Intent(this, MyService.class);stopService(intent);

}}

}

Page 51: Android Basic Components

public class MyService extends Service implements Runnable {

private boolean isRunning;

private final static String TAG = "MyService";

private Thread thread;

@Override

public IBinder onBind(Intent arg0) {

return null;

}

@Override

public void onDestroy() {

isRunning = false;

}

@Overridepublic void public int onStartCommand(Intent intent, int flags, int startId) {if(!isRunning)

thread.start();return START_STICKY;

}

@Overridepublic void onCreate() {isRunning = false;thread = new Thread(this);

}

@Overridepublic void run() {isRunning = true;while(isRunning) {

try {Log.d(TAG,  "Service  running...");

Thread.sleep(1000);} catch (InterruptedException e) {

e.printStackTrace();}

}}

}

Page 52: Android Basic Components

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="fi.tamk"android:versionCode="1"android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".CallMe"

android:label="@string/app_name"><intent-filter>

<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />

</intent-filter></activity>

<service android:enabled="true" android:name=".MyService" />

</application>

</manifest>

Page 53: Android Basic Components
Page 54: Android Basic Components

IntentService

• IntentService has  separate  thread  built  in!• Class  inherites IntentService,  overrides  onHandleIntent()

• Everything  is  done  on  separate  thread• Cannot  directly  interact  with  UI

Page 55: Android Basic Components

Examplepublic class RSSPullService extends IntentService {

@Overrideprotected void onHandleIntent(Intent workIntent) {

// Gets data from the incoming IntentString dataString = workIntent.getDataString();...// Do work here, based on the contents of

dataString...

}}

Page 56: Android Basic Components

About  Bound  Services

• Create  bound  service,  if  you  want  to– Interact  with  the  service  from  activities  or  other  components

– Expose  some  of  app’s  functionality  to  other  apps  (IPC)

• To  create  bound  service,  implement  onBind()• A  bound  service  typically  lives  only  while  it  serves  another  application  component  and  does  not  run  in  the  background  indefinitely

Page 57: Android Basic Components
Page 58: Android Basic Components

Creating  a  Bound  Service

• Private  Service  for  your  own  app– Extend  Binder  class– Example:  music  application  that  needs  to  bind  an  activity  to  its  own  service  that's  playing  music  in  the  background.

• Service  for  other  apps  (work  across  processes)– Use  a  Messenger

Page 59: Android Basic Components

Extending  Binder

• If  your  service  is  private  to  your  app,  you  can  use  Binder

• Binder?  Defines  programming  interface  that  clients  can  use

• Binder  can– Contain  public  method  that  the  client  can  call– Return  the  Service  object  itself,  so  all  the  public  methods  from  the  service  is  available

Page 60: Android Basic Components

How?

• Service– Create  the  Binder  object  in  your  Service– Return  the  object  in  onBind()  method

• Client– Receive  Binder  object  from  onServiceConnected -­‐method

Page 61: Android Basic Components

public class MyService extends Service {

private IBinder mBinder;

@Overridepublic IBinder onBind(Intent intent) {

// Returns IBinder, which "wraps" MyService inside..return mBinder;

}

@Overridepublic void onDestroy() {

Log.d("MyService", "onDestroy()");}

@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {

Log.d("MyService", "onStart()");

return START_STICKY;}

@Overridepublic void onCreate() {

Log.d("MyService", "onCreate()");

mBinder = new LocalBinder(this);}

public void someMethod() {Toast.makeText(this, "someMethod is called!", 1000).show();

}}

Page 62: Android Basic Components

Client

• Client  can  bind  to  service  by  calling  bindService()

• bindService()  – method  needs  ServiceConnection object,  which  monitors  the  connection  of  the  service

• bindService()  returns  immediately,  but  ServiceConnection’s onServiceConnected is  called  to  deliver  the  Binder  to  the  client

Page 63: Android Basic Components

Examplepublic class LocalServiceExample extends Activity {

private MyService mBoundService;private MyServiceConnection mConnection;

public class MyServiceConnection implementsServiceConnection {

@Overridepublic void

onServiceConnected(ComponentName className, IBinder service) {

mBoundService = ((LocalBinder)service).getService();

}

@Overridepublic void

onServiceDisconnected(ComponentName arg0) {mBoundService = null;

}

}

@

Overridepublic void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.main);

mConnection = new MyServiceConnection();doBindService();

}

void doBindService() {Intent i = new Intent(this, MyService.class);bindService(i, mConnection,

Context.BIND_AUTO_CREATE);}

void doUnbindService() {unbindService(mConnection);

}

protected void onDestroy() {super.onDestroy();doUnbindService();

}

public void click(View v) {mBoundService.someMethod();

}}

Page 64: Android Basic Components

Application  Components

1. Activities2. Services3. Broadcast  receivers4. Content  provides

Page 65: Android Basic Components

3.  Broadcast  Receivers

• A  broadcast  receiver  is  a  component  that  does  nothing  but  receive  and  react  to  broadcast  announcements

• Your  app  can– 1)  Receive  and  react  to  system  services  (example:  battery  low)

– 2)  Receive  and  react  to  other  apps  broadcast  announcements  

– 3)  Initiate  broadcasts  to  other  apps• App  is  given  fixed  amount  of  time  to  react  on  the  broadcast!  (10  secs!)

Page 66: Android Basic Components

MyBroadCastReceiver

BroadCastReceiver

onReceive(..)Some

System  Servicebroadcast  message

register

Page 67: Android Basic Components

Registering

• To  register  Broadcast  Receiver,  you  can– 1)  Dynamically  register  with  registerReceiver(in  code)

– 2)  Statically  public  receiver  and  <register> tag  in  AndroidManifest.xml

• Note:  if  you  are  doing  this  dynamically  and  you  are  working  in  local,  you  don't  have  modify  manifest  – file.

Page 68: Android Basic Components

1.  Registering  in  Codepublic class Main extends Activity {

MyBroadCastReceiver s;IntentFilter filter;

@Overridepublic void onResume() {

super.onResume();

filter = new IntentFilter("android.intent.action.TIME_TICK");s = new MyBroadCastReceiver();registerReceiver(s, filter);

}@Overridepublic void onPause() {

super.onPause();unregisterReceiver(s);

}}

Page 69: Android Basic Components

BroadcastReceiverpublic class MyBroadCastReceiver extends BroadcastReceiver {

@Overridepublic void onReceive(Context context, Intent intent) {

Log.d("IncomingReceiver", "Time Tick");}

}

Page 70: Android Basic Components

2.  Registering  Broadcast  Receiver  in  Manifest

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="fi.tamk"android:versionCode="1"android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".Main"

android:label="@string/app_name"><intent-filter>

<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />

</intent-filter></activity>

<receiver android:name=".MyBroadCastReceiver" android:enabled="true"><intent-filter>

<action android:name="android.intent.action.TIME_TICK" /></intent-filter>

</receiver>

</application><uses-sdk android:minSdkVersion="8" />

</manifest>

Page 71: Android Basic Components

Sending  Broadcast

• Broadcast  is  sent  using  Intent  and  sendBroadcast or  sendOrderedBroadcastmethods

• Example:Intent intent = new Intent("fi.tamk.DETECTION");

sendBroadcast(intent);

Page 72: Android Basic Components

Normal  vs.  Ordered  Broadcast

• Normal  Broadcasts– Sent  with  sendBroadcast.  All  broadcasts  are  run  in  undefined  order,  often  at  the  same  time.

• Ordered  Broadcasts– Sent  with  sendOrderedBroadcast.  Each  receiver  executes  in  turn.  Possible  to  propagate  a  result  to  next  receiver.  Order  can  be  controlled  using  android:prioritytag.

Page 73: Android Basic Components

Receiver  Lifecycle

• Broadcast  Receiver  object  is  valid  only  for  the  duration  of  the  onReceive(Context,   Intent)  method– Before  API  Level  11:

• You  cannot  do  anything  asynchronous  in  here!– After  API  level  11  (3.0  -­‐>)

• You  can  use  method  goAsync() to  create  asynchronous  processing.   See:

• http://justanapplication.wordpress.com/tag/goasync/– You  can  always  start  a  service

Page 74: Android Basic Components

Registering  Broadcast  Receiver  in  Java

public class Main extends Activity implements OnClickListener {

public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

Button click = new Button(this);click.setText("Click me!");setContentView(click);click.setOnClickListener(this);

// Registering MyBroadCastReceiver to receive BroadcastsIntentFilter filter = new IntentFilter("fi.tamk.DETECTION");MyBroadCastReceiver s = new MyBroadCastReceiver();registerReceiver(s, filter);

}

@Overridepublic void onClick(View v) {

// Sending BroadcastIntent intent = new Intent("fi.tamk.DETECTION");sendBroadcast(intent);

}}

Page 75: Android Basic Components

Registering  Broadcast  Receiver  in  Manifest

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="fi.tamk"android:versionCode="1"android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".Main"

android:label="@string/app_name"><intent-filter>

<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />

</intent-filter></activity>

<receiver android:name=".MyBroadCastReceiver" android:enabled="true"><intent-filter>

<action android:name="fi.tamk.DETECTION" /></intent-filter>

</receiver>

</application><uses-sdk android:minSdkVersion="8" />

</manifest>

Page 76: Android Basic Components

Application  Components

1. Activities2. Services3. Broadcast  receivers4. Content  provides

Page 77: Android Basic Components

4.  Content  Providers

• A  content  provider  makes  a  specific  set  of  the  application's  data  available  to  other  applications

• =>  Share  data  to  other  apps• Any  app  with  appropriate  permission,  can  read  and  write  the  data.

• Many  native  databases  are  available  via  the  content  providers,  for  example  Contact  Manager

• Files,  SQL  database• Common  interface  for  querying  the  data

Page 78: Android Basic Components

About  Content  Provides

• The  result  of  the  query:  simple  table  in  Query  object

• Content  provider  exposes  a  public  URI  that  uniquely  identifies  its  data  set– URIs  begin  with  content://– Android  provides  constants  for  native  content  providers,  for  example:• ContactsContract.Contacts.CONTENT_URI

Page 79: Android Basic Components

Querying  Native Content  Provider

• You  need– URI

• ContactsContract.Contacts.CONTENT_URI– Names  of  data  fields  (result  comes  in  table)

• ContactsContract.Contacts.DISPLAY_NAME– Data  types  of  those  fields

• String

• Remember  to  modify  the  manifest  file  for  permissions!

Page 80: Android Basic Components

QueryCursor cur = managedQuery(ContactsContract.Contacts.CONTENT_URI, // What Content Provider

null, // Which columns to return (all columns)

null, // Which rows to return (all rows)

null, // Selection arguments (none)

null); // In what order

if (cur.moveToFirst()) {

// Get column DISPLAY_NAME

int nameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

do {

// Get the field values

String name = cur.getString(nameColumn);

System.out.println(name);

} while (cur.moveToNext());

}

Page 81: Android Basic Components

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

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

package="fi.tamk"

android:versionCode="1"

android:versionName="1.0">

...

<uses-permission android:name="android.permission.READ_CONTACTS">

</uses-permission>

</manifest>

Page 82: Android Basic Components

Implementing  your  own  Content  Provider

1. Set  up  a  system  for  storing  dataFor  example,  SQLite  or  flat  file

2. Extend  ContentProvider class3. Declare  the  Content  Provider  in  manifest

Page 83: Android Basic Components

2.  Extend  Content  Provider

public class MyContentProvider extends ContentProvider {

public static final Uri CONTENT_URI = Uri.parse("content://fi.tamk.phonenumber");

@Override

public int delete(Uri uri, String selection, String[] selectionArgs) {...}

@Override

public String getType(Uri uri) {...}

@Override

public Uri insert(Uri uri, ContentValues values) {...}

@Override

public boolean onCreate() {...}

@Override

public Cursor query(Uri uri, String[] projection, String selection,

String[] selectionArgs, String sortOrder) {...}

@Override

public int update(Uri uri, ContentValues values, String selection,

String[] selectionArgs) {...}

}

Page 84: Android Basic Components

3.  Manifest

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

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

package="fi.tamk"

android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".CallMe"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<provider android:name=".MyContentProvider"

android:authorities="fi.tamk.phonenumber"></provider>

</application>

<uses-sdk android:minSdkVersion="8" />

</manifest>

Page 85: Android Basic Components

Querying  the  Content  Provider

• Native  Content  Provider– Cursor cur = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

• My  own  provider– Cursor cur = managedQuery(MyContentProvider.CONTENT_URI, null, null, null, null);