Android App Components
Android App Components
Content Provider
Data Abstraction
API
ContentProvider
ContentResolver
Cursor and ContentValues
Uri and UriMatcher
Authority
Path
onCreate
getType
query
insert
update
delete
Use ContentResolver
onCreate
getType
query
insert
update
delete
Browser
Calendar
Contacts
CallLog
MediaStore
UserDictionary
Services
Archive data
Authenticate users
Example
public class NotesDataSyncService extends Service {
public void onCreate() {
}
public int onStartCommand(Intent intent,int flags,int startId){
Toast.makeText(this,"Service starting",Toast.LENGTH_SHORT).show();
return START_NOT_STICKY;
}
public IBinder onBind(Intent intent){
return null;
}
}
Service Types
Started
explicitly stopped
killed by the system but may be restarted based upon restart behavior and
any pending requests
calls onStartCommand
Service Types
Bound
client-server communication
service acts as the server
Multiple components may bind to a service at once
Destroys when all bound components unbind
};
showMessage(dataService.getStatus());
Some Considerations
Broadcast Receivers
Broadcasts
Examples
android.intent.action.BOOT_COMPLETED
android.intent.action.BATTERY_LOW
Broadcast Receivers
Broadcast receiver
An event listener
Registered to receive broadcasted intents
Runs in background may send UI notifications
Invoke a service
Only start
Cannot bind
<receiver android:name="ConnectivityReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
SMS Example
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
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smd.messenger"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="SimpleMessenger"
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="SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>