-1

I've just started using BroadcastReceiver (which may be my problem). I am trying to create a view from a service (yes, yes i know you cant) so i was sending the request to an invisible activity to create a visible view when i needed. it wasn't working as i wanted so i looked into BroadcastReceivers. So far everything is working with the exception that i cant compare my getStringExtra value to anything. Example) say i want to pass the variable "param" with a value of String "CREATE" to my BroadcastReceiver. It arrives to my receiver fine and with logcat i can see that indeed method is "CREATE". but if i want to check what param is with an if statement if (param=="CREATE") it never enters the if. So what am i doing wrong here?

public class Receiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context c, Intent i) {
        String stuff = i.getStringExtra("param");
        log(stuff);
        if (stuff=="CREATE"){
            log("why dem feels");
        }
        if((String)i.getStringExtra("method")=="CREATE"){
            log("i dont know what im doing");
        }
        log("FOR SOME REASON ITS SKIPPING THIS STEP");
    }



    //set of logs so that i dont have to keep specifying tags and shit
        public void log(int in){
               Log.d("TAG OF INTEGERS",Integer.toString(in));
           }

           public void log(String s){
               Log.d("Receiver",s);
           }
           public void log(float f){
               log((int) f);
           }

}

This is the code that i am using in my service to send out the broadcast

Intent i = new Intent();
           i.setClassName("com.example.starteroverlay","com.example.starteroverlay.Receiver");
           i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           String methodCall = "CREATE";
           i.putExtra("param",methodCall);
           //
           sendBroadcast(i);

and just because i figure you guys will want to see the part of my manifest with the code

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

 <receiver android:name=".Receiver">
            <intent-filter android:priority="0">
                <action android:name="android.intent.action.MAIN"/>
            </intent-filter>
        </receiver>
miversen33
  • 573
  • 5
  • 19

3 Answers3

3

use equals method always it will check the content of the String but == check only object reference

if (method.equals("CREATE"))
{
      //do what you need
}
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • no joke, i spent a good couple hours looking for that. i was searching on a much smaller scale though looking about why my intent could be failing or why my receiver wasnt getting what it needed. lol i needed to check out strings instead. thank you much for that sunil – miversen33 Oct 03 '13 at 05:30
2

use equals() instead of == to compare Strings

So change your code to

 if (method.equals("CREATE"))

Check http://docs.oracle.com/javase/tutorial/java/data/comparestrings.html

PS.

Change your code To

if (stuff.equals("CREATE")){
     log("why dem feels");
  }
if((String)i.getStringExtra("method").equals("CREATE")){
    log("i dont know what im doing");
 }
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
0
// try this
@Override
    public void onReceive(Context c, Intent i) {
        String stuff = i.getStringExtra("param");
        if (stuff.equals("CREATE")){
           // write your code here
        }
    }
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67