BroadcastReceiver(Incoming call)
February 2, 2012
Broadcast receiver and Pending Intent
—————————————-
A broadcast receiver is a class which extends BroadcastReceiver and which is
—————————————-
A broadcast receiver is a class which extends BroadcastReceiver and which is
registered as a receiver in an Android Application via the AndroidManifest.xml
file(or via code).
The class 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.
Therefore you cannot perform any asynchronous operation.
Pending Intent
————–
A PendingIntent is a token that you give to another application,which allows
————–
A PendingIntent is a token that you give to another application,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().
Broadcast Receiver Example
—————————
We will define a broadcast receiver which listens to telephone state changes.
—————————
We will define a broadcast receiver which listens to telephone state changes.
If the phone receives a phone call then our receiver will be notified and log a
message.
In Androidmanifest.xml file
—————————
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.broadcastreceiverdemo”
android:versionCode=”1″
android:versionName=”1.0″>
<application android:icon=”@drawable/icon” android:label=”@string/app_name”>
<receiver android:name=”.BroadcastReceiverdemo”>
<intent-filter>
<action android:name=”android.intent.action.PHONE_STATE”></action>
</intent-filter>
</receiver>
</application>
<uses-permission android:name=”android.permission.READ_PHONE_STATE”></uses-permission>
</manifest>
—————————
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.broadcastreceiverdemo”
android:versionCode=”1″
android:versionName=”1.0″>
<application android:icon=”@drawable/icon” android:label=”@string/app_name”>
<receiver android:name=”.BroadcastReceiverdemo”>
<intent-filter>
<action android:name=”android.intent.action.PHONE_STATE”></action>
</intent-filter>
</receiver>
</application>
<uses-permission android:name=”android.permission.READ_PHONE_STATE”></uses-permission>
</manifest>
In .java file
————-
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class BroadcastReceiverdemo extends BroadcastReceiver
{
————-
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class BroadcastReceiverdemo extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w(“DEBUG”, state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.w(“DEBUG”, phoneNumber);
Toast.makeText(context, “hai”, Toast.LENGTH_LONG).show();
}
}
}
}
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w(“DEBUG”, state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.w(“DEBUG”, phoneNumber);
Toast.makeText(context, “hai”, Toast.LENGTH_LONG).show();
}
}
}
}
沒有留言:
張貼留言