0

I have to unlock a feature of my app (to the users) only when they install an application (app link is present in my app; when button is clicked the play store link of the app opens).

NOTE :- the application should not be installed in the phone previously. If installed user must be asked to uninstall the app. Only when the user uninstalls the app, the playstore link should be made available.

I have tried this code, but it has a problem. Problem is - if the user clicks the button(ie the link) the FEATURE is UNLOCKED no matter if he/she installs the app or not.

TextView tv, tvad;
Button bcheck;
int p=0;
String x="";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    boolean installed = appInstalledOrNot("com.ankushkapoor2015.kiittimetable");
    tv = (TextView) findViewById(R.id.tvCheck);
    tvad = (TextView) findViewById(R.id.tvad);
    bcheck = (Button) findViewById(R.id.button);

    try
    {
        SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
        x = sharedPreferences.getString("CHECK", "");
        System.out.println("FILE READ");
    }
    catch (Exception e)
    {
        System.out.println("FILE NOT READ");
    }
    System.out.println(x + " Hello");
    if(x.equalsIgnoreCase("Installed"))
        tvad.setText("UNLOCK THE FEATURE");
    else
        tvad.setText("LOCK THE FEATURE");

    if(installed) {
        tv.setText("Installed - Uninstall App"+p);
        bcheck.setVisibility(View.INVISIBLE);
        System.out.println("App is already installed on your phone");
    } else {
        tv.setText("Not Installed "+p);
        System.out.println("App is not currently installed on your phone");
    }
    bcheck.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            p=1;//app installed
            try {
                SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                String s = "Installed";
                editor.putString("CHECK", s);
                editor.commit();
                System.out.println("FILE CREATED");
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.ankushkapoor2015.kiittimetable"));
                startActivity(browserIntent);
            }
            catch (Exception e)
            {
                System.out.println("FILE NOT CREATED");
            }
        }
    });
}
private boolean appInstalledOrNot(String uri) {
    PackageManager pm = getPackageManager();
    boolean app_installed;
    try {
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        app_installed = true;
    }
    catch (PackageManager.NameNotFoundException e) {
        app_installed = false;
    }
    return app_installed;
}
  • Is your package name the same for both apps? – OneCricketeer Mar 14 '16 at 19:41
  • @cricket_007 no both are different apps. I have to unlock a feature of my app (Let app name be XYZ) if user installs my another app (say app ABC) THROUGH the app XYZ. There are few conditions 1. If app to be installed (ABC) is already installed - user should be asked to uninstall the app; only then the link (of playstore) should me made available inside app XYZ (current app). 2. If app is not installed then no problem (I HAVE ALREADY SOLVED THIS CASE) – Ankush Kapoor Mar 14 '16 at 19:52
  • I understand they are different apps, but if you have `appInstalledOrNot ("com.ankushkapoor2015.kiittimetable")` returning true, then your app is installed. I assumed you are the creator of both apps, so you may be using the same package name for both the app that does the checking and the app you are checking for. – OneCricketeer Mar 14 '16 at 19:55
  • @cricket_007 no the package name for the current app is different and `("com.ankushkapoor2015.kiittimetable")` is different. – Ankush Kapoor Mar 14 '16 at 20:22
  • So maybe I don't fully understand what the code is doing, but from what I see, there is nothing preventing the user from clicking the button and always going to the Play Store. – OneCricketeer Mar 14 '16 at 20:29

1 Answers1

0

Adding the below statement in the if else worked

if(x.equalsIgnoreCase("Installed")&&installed==true)//installed==true added 
        tvad.setText("UNLOCK THE FEATURE");
    else
        tvad.setText("LOCK THE FEATURE");