Reputation: 89
I am trying to append/update information in a TextView
from a BroadcastReceiver
. What would be the right way to do this? I have put in in my Manifest to register the phone state:
<receiver android:name="RingTypeBroadcastReceiver" >
<intent-filter>
<action android:name= "android.intent.action.PHONE_STATE"> </action>
</intent-filter>
</receiver>
Next I want to grab the phone state that the receiver picked up, increment a counter. I want to be able to append/update this information on a textview
called txtResults
that I have on my Activity
.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class RingTypeBroadcastReceiver extends BroadcastReceiver{
int counterCall = 0;
int counterIdleOrOffHook = 0;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
counterCall = counterCall + 1;
}
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)
|| intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
counterIdleOrOffHook = counterIdleOrOffHook + 1;
}
}
}
Upvotes: 0
Views: 1247
Reputation: 33856
Put your broadcast receiver inside your activity that is setting the text. (sub class it)
Here is an untested example. It might need slight modification, but I have done something like this before so it will work.
public class YourActivity extends Activity {
private TextView txtResults;
....do activity stuff....
public static class RingTypeBroadcastReceiver extends BroadcastReceiver{
int counterCall = 0;
int counterIdleOrOffHook = 0;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
counterCall = counterCall + 1;
}
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)
|| intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
counterIdleOrOffHook = counterIdleOrOffHook + 1;
}
//here you can set your text view. But check that it is not null first!
if(txtResults != null){
txtResults.setText("whatever");
}
}
}
Then, update your Manifest so it will point to the Broadcast receiver:
<receiver android:name="YourActivity$RingTypeBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</receiver>
As you can see, you have your Activity followed by a $
then your broadcast receiver. That tells the manifest to look inside your activity.
Also note: If your activity is in a package below your app's package name (com.example.yourapp
) you will need to add that to your manifest. So for instance, if your activity was in com.example.yourapp/activity
you would need to change the name in the manifest to .activity.YourActivity$RingTypeBroadcastReceiver
. It is just everything after your apps package name, starting with .
.
Upvotes: 1