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;
}