A broadcast receiver is a class which extends "BroadcastReceiver" and which is registered as a receiver in an Android Application via the AndroidManifest.xml (or via code). This class will be able to receive intents via the sendBroadcast() method. "BroadCastReceiver" defines the method "onReceive()". Only during this method your broadcast receiver object will be valid, afterwards the Android system will consider your object as no longer active. Therefore you cannot perform any asynchronous operation.
Pending Intent
This tutorial will also use a PendingIntent. A PendingIntent is a token that you give to another application (e.g. Notification Manager, Alarm Manager or other 3rd party applications), which allows this other application to use the permissions of your application to execute a predefined piece of code.
To perform a broadcast via a pending intent so get a PendingIntent via PendingIntent
.getBroadcast(). To perform an activity via an pending intent you receive the activity via PendingIntent.getActivity().
We will define a broadcast receiver which listens to telephone state changes. If the phone recei
ves a phone call then our receiver will be notified and log a message.
Create a new project "de.vogella.android.receiver.phone". We do not need an activity. Create the following "AndroidManifest.xml".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.vogella.android.receiver.phone" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name="MyPhoneReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
</manifest>
Create the following class "MyPhoneReceiver".
If you install your application and receive a call, e.g simulated by the DDMS perspective in Eclipse, then your receiver will be called and lot a message to the console.
System Services and Broadcast Receiver
In this chapter we will use the AlertManager and VibratorManager. The VibratorManager will be called by the broadcast receiver which will be called by the AlertManager.
Maintain this class as broadcast receiver in "AndroidManifest.xml" and allow the vibrate authorization.Define and consume your own service
The following will demonstrate how to create and consume a service from an activity. The service will periodically fetch data. The service will used by an activity which bind itself to the service. The activity will allow to request the latest data from the service.
Create a new project "de.vogella.android.ownservice" with the activity "ServiceConsumer".
Create a service "WordService" by create the class and the entry in "AndroidManifest.xml".
package de.vogella.android.ownservice;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class WordService extends Service {
private Timer timer = new Timer();
private static final long UPDATE_INTERVAL = 5000;
private final IBinder mBinder = new MyBinder();
private ArrayList
private String[] fixedList = { "Linux", "Android", "iPhone", "vogella.de",
"helpful", "stuff" };
private int index = 0;
public void onCreate() {
super.onCreate();
pollForUpdates();
}
private void pollForUpdates() {timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// Imagine here a freaking cool web access ;-)
if (list.size() >= 6) {
list.remove(0);
}
list.add(fixedList[index++]);
if (index >= fixedList.length) {
index = 0;
}
}
}, 0, UPDATE_INTERVAL);
Log.i(getClass().getSimpleName(), "Timer started.");
}
@Override
public void onDestroy() {
super.onDestroy();
if (timer != null) {
timer.cancel();
}
Log.i(getClass().getSimpleName(), "Timer stopped.");
}
// We return the binder class upon a call of bindService@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
public class MyBinder extends Binder {
WordService getService() {
return WordService.this;
}
}
public Listreturn list;
}
}
great job
ReplyDelete