This is True / False or a bug!?

by Perry168 » Thu, 21 Apr 2011 09:55:51 GMT


Sponsored Links
 Hi,
Today, I wrote a program. The function is to get the installed
packages name through the provider name. But I find a problem, when I
compare two SAME strings. The Boolean result will be FALSE.
I used the Eclipse to watch the value change. From expression, I saw
the Local1 and xx are same. But the Boolean bb is false. Also I
installed the apk to phone too. I found the result is "FALSE" too.
I don know the problem come from and how to fix it.
I hope who can teach me.
Following is my main.XML and source.

 < XML >

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/ 
android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView android:text="TextView" android:id="@+id/tV1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
    <Button android:text="Button" android:id="@+id/bn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>

< JAVA SOURCE >
import java.util.List;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.ProviderInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class FindPackage extends Activity  implements OnClickListener
{

    TextView tV1;
    Button bn1;
        String xx = "com.android.alarmclock";
        Boolean bb = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    tV1 = (TextView) findViewById (R.id.tV1);
    bn1 = (Button) findViewById (R.id.bn1);
    bn1.setOnClickListener (this);
    }

        @Override
        public void onClick(View v) {

        if (v == bn1){
                List <PackageInfo>  aa = null;
                ProviderInfo [] kk = null;
                String Local1 = "";
                aa =  this.getPackageManager().getInstalledPackages(8); // Get 
all
Packages
                int i = aa.toArray().length-1; // Get Number of Packages


        while (i>=0)
        {
                kk = aa.get(i).providers; // Withdraw the Provider
                if (kk == null){
                        i--;
                        continue;
                }
                if (kk[0].name.contains("Alarm")) // Checking the Provider name
which has Alarm word"
                                {
                                Local1 = kk[0].packageName; // put the package 
name into
Local1
                                break;
                                }
                i--;
        }

                i = 0;

            bb = (xx==Local1);

            tV1.setText(bb+""); // Show the result

        }


        }
}

-- 



Re: This is True / False or a bug!?

by Jens » Thu, 21 Apr 2011 18:56:49 GMT


 ou could start with your broken string comparison.

(xx==Local1);

Use String#equals(String) instead if you're not trying to ascertain if
xx and Local1 are both *instances* of the same String object.

On 20 Apr, 06:08, Perry168 <perry...@netvigator.com> wrote:

--


Sponsored Links


Re: This is True / False or a bug!?

by Shawn Holland » Thu, 21 Apr 2011 18:56:54 GMT


 our code is checking to see if the two variables referencing the same
object. You need to use the equals method...

bb = (xx.equals(Local1));


On Apr 20, 12:08am, Perry168 <perry...@netvigator.com> wrote:

--



Re: This is True / False or a bug!?

by DanH » Fri, 22 Apr 2011 03:03:23 GMT


 lso, if one will be doing a lot of repeated compares of the same
String values, String.intern() can be used to get a pointer that can
be compared to other pointers to interned Strings.

Some Strings (I'm thinking all String literals in a program) are
defined to be already interned. So, eg, if ClassA.methodA calls
ClassB.methodB and passes a value that originated as the literal "Some
String!", one could (if they knew the value originated as a literal)
safely compare to the literal "Some String!" in methodB using the "=="
comparison.

On Apr 21, 5:56am, Shawn Holland <seholl...@gmail.com> wrote:

--



Re: This is True / False or a bug!?

by cool rss feed » Fri, 22 Apr 2011 03:39:22 GMT


 YI:

"==" on String will do an Object-based comparison. it means that xx
and local1 IS same OBJECT if the result value is true.

and 99% of your time you wont be doing object-based comparison on
String object. so use String.compareTo().



On Apr 20, 12:08pm, Perry168 <perry...@netvigator.com> wrote:

--



Re: This is True / False or a bug!?

by cool rss feed » Fri, 22 Apr 2011 03:39:23 GMT


 hy dont you use Local1.compareTo(xx)?

Keep in mind that "==" does not guarantee the same result when you use
String.compareTo() from program to program.

Terence

On Apr 20, 12:08pm, Perry168 <perry...@netvigator.com> wrote:

--



Re: This is True / False or a bug!?

by dalisa099 » Fri, 22 Apr 2011 03:39:24 GMT


 Hi,

Replacing the line :
bb = (xx==Local1);
by :
bb = xx.equals(Local1);

should solve your issue.


-- 



Re: This is True / False or a bug!?

by Hyvi » Fri, 22 Apr 2011 03:39:27 GMT


 b = (xx==Local1);
should be
bb = (xx.equals(Local1));

2011/4/20 Perry168 <perry...@netvigator.com>





--
========================= ===============
Protect your dream ...

wEiBo: @Blamestorming
tWiTTer: HyviTan
fAvOriTe: Linux Java

--



Other Threads

1. Regarding BT on time in Android (Eclair)

Hi All,


This mail is regarding Bluetooth ON time in Android.

I observed that BT on time takes nearly 6sec to turn on.

I found that *t_enable*" function has 3 sec delays
(system/bluetooth/bluedroid/bluetooth.c file) after starting the bluetooth
daemon.



#define HCID_START_DELAY_SEC   3



    LOGI ("Starting bluetooth deamon");

    if (property_set("ctl.start", "bluetoothd") < 0) {

        LOGE("Failed to start bluetoothd");

        goto out;

    }

*    sleep (HCID_START_DELAY_SEC);*



As per my understanding bluetooth daemon sends few Init commands (around 20
commands) to initialize BT chip like enquiry mode, page scan etc...


Really do we need 3 secs delay after bluetooth daemon started?

Can we optimize this delay less than 3 sec?



Appreciate your response.

Thanks in advance

-- 
Regards
Bheemsen

-- 
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: 

2. Tentang Foursquare

sesuai dengan subject ada yg mau ditanyakan, terkadang ketika check-in
sering dpt warning kita terlalu jauh atau kita terlalu cepat sehingga gak
dapet point. yg bikin bingung adalah, warning itu dapet pas check in pertama
kali dan memang pada posisi yg sudah seharusnya.

itu karena servernya down, gpsnya ngaco atau emg apa yah? Mungkin ada yg
mengalami hal yang sama. terima kasih


PS: bukan bantji badge, tapi emang ngejar badge segede iPad.  :)

-- 
Narendra Kameshwara

YM : kosto...@yahoo.com
http://blogroll.dailywhatnot.com/nareen/

-- 
"Indonesian Android Community [id-android]" 

3. Ask Seven

4. twitter and facebook intent : suggestion for the community

5. Messaging how to move to new lin

6. Helloé”›

7. Creating an Alert dialog in a dialog