BoadcastReceiver-tuning

Put your problem here if it does not fit any of the other categories.

BoadcastReceiver-tuning

Postby BossOss » Thu Jan 26, 2012 10:53 am

Hey guys,

i want to modify my IncomingCallReceiver that listens to the ACTION_PHONE_STATE_CHANGED tag. I want him to differentiate between the following five states:

(IDLE)Phone is idle -> (RINGING)first call incoming -> (OFFHOOK)first call active -> (RINGING)second call incoming - > (OFFHOOK)second call active/first call on hold

My onReceive method looks like this:

Code: Select all
@Override
   public void onReceive(Context context, Intent intent) {
      this.context = context;
      final Bundle extras = intent.getExtras();
      if (intent.getAction().equals(
            TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
         final String state = extras.getString(TelephonyManager.EXTRA_STATE);
         if ("IDLE".equals(state)) {
            setIstate(0);
            Log.i("IDLE", ""+iState);
         }
         if ("OFFHOOK".equals(state)) {
            if (iState == 1) {
               Log.i("OFFHOOK", "dont return!");
               return;
            }else{
               startIncomingASAP();
               setIstate(1);
               Log.i("OFFHOOK", "return!"+iState);
            }
         }
      }
   }


Actually I want the receiver NOT TO START the startIncomingASAP() function on the latter OFFHOOK phone state by adding an Integer for indicating which OFFHOOK's turn it is. But it doesn't work properly: although the latter OFFHOOK-Log.i is shown in the LogCat normally (meaning that iState was set to 1 successfully), he ignores the state and starts the functions on the second OFFHOOK again ....
My DDMS view doesn't work properly and I'm kinda clueless now :(

someone has an idea??

greetz BossOss
BossOss
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Jan 09, 2012 2:54 pm

Top

Re: BoadcastReceiver-tuning

Postby Phyll » Thu Jan 26, 2012 12:22 pm

Hi BossOss,

You should probably make sure that iState persists from one onReceive to the next.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: BoadcastReceiver-tuning

Postby BossOss » Thu Jan 26, 2012 2:13 pm

Could anyone tell me how to? I tried using the value via my Activity but the receiver doesn't do it ....

thx for any help so far
BossOss
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Jan 09, 2012 2:54 pm

Re: BoadcastReceiver-tuning

Postby BossOss » Thu Jan 26, 2012 2:17 pm

i found a way by creating a file and write the number into it then reading it out again but isn't there a way without files?

greetz
BossOss
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Jan 09, 2012 2:54 pm

Re: BoadcastReceiver-tuning

Postby Phyll » Thu Jan 26, 2012 2:38 pm

Hi BossOss,

Your choices for persisting data are preferences, files, SQLite and maybe content providers (which probably just encapsulates files or SQLite anyway). I'm not sure which one of these is exactly appropriate for your circumstances. I see nothing wrong with files. Android keeps a number of status data items in one big file that it reads from for some of its managers.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: BoadcastReceiver-tuning

Postby BossOss » Thu Jan 26, 2012 3:14 pm

I updated it with sharedpreferences to access the data and it looks like the following:

Code: Select all
@Override
   public void onReceive(Context context, Intent intent) {
      this.context = context;
      final Bundle extras = intent.getExtras();
      final SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
      setIstate(prefs.getInt("State", 0));
      SharedPreferences.Editor edit = prefs.edit();
      
      
      if (intent.getAction().equals(
            TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
         final String state = extras.getString(TelephonyManager.EXTRA_STATE);
         if ("IDLE".equals(state)) {
            setIstate(0);
            Log.i("IDLE", ""+iState);
            edit.putInt("State", iState);
         }
         if ("OFFHOOK".equals(state)) {
            if (iState == 1) {
               Log.i("OFFHOOK", "dont return!");
               return;
            }else{
               startIncomingASAP();
               setIstate(1);
               Log.i("OFFHOOK", "return!"+iState);
               edit.putInt("State", iState);
            }
         }
      }
   }


But it still doesn't work .... do you see the mistake?? otherwise I'll try something else...

greetz and thanx a priori
BossOss
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Jan 09, 2012 2:54 pm

Top

Re: BoadcastReceiver-tuning

Postby Phyll » Thu Jan 26, 2012 9:13 pm

Hi BossOss,

I think so. Here's an example of getting and saving preferences. I think you're missing the commit.

Code: Select all
    private void getMyPreferences() {
        namestore1 = prefs.getString(PREF_NAME1,"");
        namestore2 = prefs.getString(PREF_NAME2,"");
        }//getpreferences

    private void savePreferences() {
        Editor editor = prefs.edit();
        editor.putString(PREF_NAME1, namestore1);
        editor.putString(PREF_NAME2, namestore2);
        editor.commit();
        }//savepreferences


Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: BoadcastReceiver-tuning

Postby BossOss » Fri Jan 27, 2012 8:42 am

lol how foolish of me thx Phyll .... works fine now ...
BossOss
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Jan 09, 2012 2:54 pm

Re: BoadcastReceiver-tuning

Postby BossOss » Thu Feb 02, 2012 4:34 pm

Hey guys,

i wanted to tune up my bc-receiver a little bit more, by adding the possibility to return to the last window from the aspect of the present active Activity.
For my specific case:

Code: Select all
MyBroadCastReceiver:
public void onReceive(Context context, Intent intent) {
...
if (intent.getAction().equals(
               TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
            
            final String state = extras
                  .getString(TelephonyManager.EXTRA_STATE);
            number = extras.getString("incoming_number");
            Log.i("ICROnReceive","Number: " +number);
            
            if ("RINGING".equals(state)) {
               
               if (number != null) {
                  
                  if ("+491234567890".equals(number)){
                     pickup(context);
==>>                 //HERE
                  }
                  Log.i("ICROnRingingNumberNotNull","Number: " +number);
                  edit.putString("number", number);
                  edit.commit();
                  return;
                  
               }               
            }
...


At the position where the arrow points to, I want the broadcastreceiver to return to the last window, as if I would push the "Back" button on the phone ... is this possible for a bc receiver in respect to an activity at all???

greetz

BossOss
BossOss
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Jan 09, 2012 2:54 pm

Re: BoadcastReceiver-tuning

Postby Phyll » Sat Feb 04, 2012 7:07 pm

Hi BossOss,

Me again. You know I found a permission INJECT_EVENT that would allow you to send the backkey event to another process but there isn't much about how to do that.

Maybe this will help.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: BoadcastReceiver-tuning

Postby BossOss » Mon Feb 06, 2012 11:45 am

this permission is only for systemsignature signed processes - in other words it is internal and no use for sdk applications ...
bummer!

thx anyway
BossOss
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Jan 09, 2012 2:54 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 15 guests