Android App Components
Android App Components
App Components
● App Components
– Activity
– Content Provider
– Service
– Broadcast Receiver
● Messaging Components
– Intent
– Others (based upon Binder IPC)
Intent
● Intent
– An abstract depiction of the operation
– A message for the OS to perform an action
● Applicability
– Start an activity (internal or external)
– Interact with other components like services and broadcast receivers
● Types
– Explicit
– Implicit
Explicit Intent
Class reference
startActivity(intent);
startActivity(intent);
An app component can specify multiple filters representing different ways it can handle intents
Implicit Intent
● Primary Information
– Action : a string specifying an action
– Data : a URI referencing data for the action
– Category : a string containing additional information about component
● Secondary Information
– Extras
– Flags
<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.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="note" />
</intent-filter>
</activity>
static {
matcher.addURI("com.example.smd.notesprovider","notes",1);
}
if(matcher.match(uri) == 1){
return db.query("Notes",projection,selection,selectionArgs,null,null,sortOrder);
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smd"
android:versionCode="1"
android:versionName="1.0">
<activity android:name="NotesActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.smd.ListActivity"
android:label="ListActivity"
android:parentActivityName="com.example.smd.NotesActivity" >
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.smd.NotesActivity" />
</activity>
<provider android:name="NotesProvider"
android:authorities="com.example.smd.notesprovider"
android:exported="true" />
</application>
</manifest>
Using a Content Provider
● Use ContentResolver
– A remote proxy object for the provider
– Provides same methods as provided by the provider
● onCreate
● getType
● query
● insert
● update
● delete
public class AppTest extends Activity
{
...
while (cursor.moveToNext()){
result += cursor.getString(cursor.getColumnIndex("title")) + "\n";
}
Toast.makeText(this,result,Toast.LENGTH_SHORT).show();
}
}
}
Standard Content Providers
● Browser
● Calendar
● Contacts
● CallLog
● MediaStore
● UserDictionary
Services
● Application component without a UI
● A long-running operation in the background
● Continues running even if user switches to
another application
● May be invoked remotely from a different
process through IPC
Example Usage Scenarios
● Download / Upload / Synchronize data on network
● Alarms / notifications / reminders (on some specific event)
● Send emails / messages in a fail-safe manner
● Play audio / music in the background
● Archive data
● Authenticate users
● Other application specific scenarios
Example
public class NotesExportService extends Service {
}
Service Types
● Started
– Performs a background operation and does not return
any result to the caller
– Once started, may run indefinitely unless
● explicitly stopped
● killed by the system but may be restarted based upon restart
behavior and any pending requests
– Useful for cases like syncing data, playing audio, etc.
– Invocation
● from other App components (Activities or BroadcastRecievers)
using explicit intents
Intent intent = new Intent(this,NotesExportService.class);
startService(intent);
● calls onStartCommand
Service Types
● Bound
– Bound to the application component starting the service
● client-server communication
● service acts as the server
– Runs only as long as another component is bound to it
● Multiple components may bind to a service at once
● Destroys when all bound components unbind
– Useful for cases like authentication, play audio with
status updates, etc.
– Invocation
● Using Binder, ServiceConnection and bindService
● calls onBind
public class NotesExportService extends Service {
}
public class SettingsActivity extends BaseActivity
{
NotesExportService dataService;
boolean bound = false;
...
private ServiceConnection connection = new ServiceConnection(){
<receiver android:name="ConnectivityReceiver">
<intent-filter>
<action
android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Example broadcast receiver registered dynamically in an activity
ConnectivityReceiver receiver;
…
SharedPreferences preferences =
context.getSharedPreferences("service",Context.MODE_PRIVATE);
boolean started = preferences.getBoolean("started",false);
SharedPreferences.Editor editor = preferences.edit();
if(!started){
Intent serviceIntent = new Intent(context,NotesDataSyncService.class);
context.startService(serviceIntent);
editor.putBoolean("started",true);
}
else{
Intent serviceIntent = new Intent(context,NotesDataSyncService.class);
context.stopService(serviceIntent);
editor.putBoolean("started",false);
}
editor.commit();
}
}
SMS Example
● SMS received broadcast
– android.provider.Telephony.SMS_RECEIVED
– Ordered broadcast – may be aborted by some
application
– Requires permissions
● android.permission.RECEIVE_SMS
● android.permission.READ_SMS
– Intent structure
● Generally carries an array of PDUs, mapped to string pdus
public class SmsReceiver extends BroadcastReceiver {
try {
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
}
}
} catch (Exception e) {
}
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smd.AppTest"
android:versionCode="1"
android:versionName="1.0">
</manifest>