Help with a BraodcastReceiver

Discussion in 'Android App Developers' started by r2sjoblom, Dec 27, 2011.

  1. r2sjoblom
    Offline

    r2sjoblom New Member

    Joined:
    Feb 24, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    My situation is that I want to call a particular method to get called from ClassA from inside my ClassB which extends the BroadcastReceiver class. I understand how to do this from inside another class file etc and how to setup the BroadcastReceiver, or so I thought.
    Below is the class file that extends the BroadcastReceiver.
    Code:
     public class WatchPhone extends BroadcastReceiver {
    [COLOR=#00008b]private[/COLOR] [COLOR=#2b91af]ClassA[/COLOR] classA = new ClassA(); 
     
    [COLOR=#800000]@Override[/COLOR] 
    [COLOR=#00008b]public[/COLOR] [COLOR=#00008b]void[/COLOR] onReceive([COLOR=#2b91af]Context[/COLOR] context, [COLOR=#2b91af]Intent[/COLOR] [COLOR=#2b91af]BackToMain[/COLOR]) { 
        [COLOR=#808080]// TODO Auto-generated method stub[/COLOR] 
        [COLOR=#2b91af]TelephonyManager[/COLOR] telephony = ([COLOR=#2b91af]TelephonyManager[/COLOR])context.getSystemService([COLOR=#2b91af]Context[/COLOR].TELEPHONY_SERVICE);  
        [COLOR=#00008b]int[/COLOR] state = telephony.getCallState();  
        [COLOR=#00008b]switch[/COLOR](state) {  
            [COLOR=#00008b]case[/COLOR] [COLOR=#2b91af]TelephonyManager[/COLOR].CALL_STATE_RINGING: 
                [COLOR=#2b91af]Log[/COLOR].d([COLOR=#800000]"TestServiceReceiver"[/COLOR], [COLOR=#800000]"IDLE"[/COLOR]);  
                classA.[COLOR=#2b91af]IwantToDoThis[/COLOR](); 
                [COLOR=#00008b]break[/COLOR];  
        } 
    } 
    
    The method that I am trying to call does work great in the application if I manually request it. In my AndroidManifest.xml I added this

    Code:
        [COLOR=#800000]<uses-permission[/COLOR] [COLOR=#ff0000]android:name[/COLOR]=[COLOR=#0000ff]"android.permission.READ_PHONE_STATE"[/COLOR][COLOR=#800000]/>[/COLOR] 
    
    and this at the bottom just above the closing application tag.
    Code:
    
                [COLOR=#800000]<receiver[/COLOR] [COLOR=#ff0000]android:name[/COLOR]=[COLOR=#0000ff]".WatchPhone"[/COLOR][COLOR=#800000]>[/COLOR] 
                        [COLOR=#800000]<intent-filter>[/COLOR]  
                        [COLOR=#800000]<action[/COLOR] [COLOR=#ff0000]android:name[/COLOR]=[COLOR=#0000ff]"android.intent.action.PHONE_STATE"[/COLOR] [COLOR=#800000]/>[/COLOR]  
                        [COLOR=#800000]</intent-filter>[/COLOR]  
                [COLOR=#800000]</receiver>[/COLOR] 
    
    When the phone rings, the application crashes then closes. Any ideas on what I should do.