If statements being ignored when checking package names

Diija :

I'm retrieving the name of a package and then trying to do something depending on what the package name is, but when I use an if statement to see if the package is what I'm looking for it is ignored and the code runs the else statement instead. If it changes anything, this is all being run in an asynctask in a foreground service and is being run on android API 19.

private class findActiveApp extends AsyncTask<Void, Void, String>{
        PackageManager pm = getApplicationContext().getPackageManager();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected String doInBackground(Void... voids) {

            String s;
            ComponentName activeApp = topApp(); //Retrieves the currently running application
            try{
               s = activeApp.getPackageName();  //Gets the package name of the application
            }catch(Exception e){
                e.printStackTrace();
                s = "null";
            }
            Log.d("processName", "S IS CURRENTLY: "+ s + " :END");
            if(s == "com.dmitrijoss.geolock"){
                Log.d("testingPackageName", "inside if");
                Log.d("testingPackageName", "S IS CURRENTLY: "+ s);
            }
            else if(s == "com.android.launcher"){
                Log.d("testingPackageName", "inside if");
                Log.d("testingPackageName", "S IS CURRENTLY: "+ s);
            }
            else{
                Log.d("testingPackageName", "inside else");
                Log.d("testingPackageName", "S IS CURRENTLY: "+ s);
                redirect();
            }
            return null;
        }
    }

And this code returns

03-10 19:43:23.094 7587-7629/com.dmitrijoss.geolock D/testingPackageName: inside else
03-10 19:43:23.094 7587-7629/com.dmitrijoss.geolock D/testingPackageName: S IS CURRENTLY: com.dmitrijoss.geolock

which means it is ignoring the first if statement where the log should be activating instead.

DanMoranDev :

Replace s == "com.dmitrijoss.geolock"

with

s.equals("com.dmitrijoss.geolock")

This is because the == operator compares the object reference, while equals() compares the content of the string.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=193321&siteId=1