1

I am looking to find the first Install time of different applications installed on my device using my app. The very basic PackageManager->PackageInfo->firstInstallTime field will suffice but it is always returning NameNotFoundException even though I have tried multiple app's(correct) package names.


    package com.example.testingpackageinfo;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.content.pm.PackageInfoCompat;
    
    import android.content.Context;
    import android.content.Intent;
    import android.content.pm.PackageInfo;
    import android.content.pm.PackageManager;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
        Button button ;
        TextView textView;
        Context context;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            context = this;
    
    
            button = findViewById(R.id.button);
            textView = findViewById(R.id.textView);
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    try {
                        PackageManager pm = context.getPackageManager();
                        PackageInfo pInfo = pm.getPackageInfo("com.google.android.youtube", 0);
                        long installTime = pInfo.firstInstallTime;
                        textView.setText(String.valueOf(installTime));
                    } catch (PackageManager.NameNotFoundException e) {
                        e.printStackTrace();
                        textView.setText("Package Not found");
                    }
                }
            });
    
    
        }
    }

  • Can you check this issue: https://stackoverflow.com/questions/62345805/namenotfoundexception-when-calling-getpackageinfo-on-android-11 – Yasin Ege Nov 03 '22 at 12:56

1 Answers1

1

Try this :

 private boolean checkInstalledApp(String packageName, PackageManager packageManager) {
        try {
            packageManager.getPackageInfo(packageName, 0);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }