Android Tutorial How to control how many installations of an App

The interesting 'Trial Tutorial' by XverheistX has given me another idea (non market app)

Example: It could theoretically happen that you want to sell an application to a given Company and have fixed a price for a certain number of licences. Once the apk is sold, you (or the Company self) want to have a control on the effective number of installations. And maybe also know how many times a single user have uninstalled and reinstalled the app too.
This is the way I've tried to do it with a MySql database.
'Burgeland' is a fantasy Company name.
Only one web connection is required at the first app installing, no more till you unistall and reinstall the app.
The Database have one table (licenze) and three fields(id,cliente,imei,installato).

This demo code shows some MsgBox that users should not see in real implementation.


ctrllicence.php
B4X:
<?php
$hst  = "xxx";
$db = "xxx";
$usr = "xxx";
$pwd = "xxx";
$comp = $_GET['company'];
$imi= $_GET['imei'];

$conn_DB=mysql_connect($hst,$usr,$pwd) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

$query="SELECT * FROM licenze WHERE cliente = '$comp' AND imei = '$imi'";
$result=mysql_query($query,$conn_DB) or die(mysql_error());
$numrows=mysql_num_rows($result);

If ($numrows == 0) {
$queryINS="INSERT INTO licenze (cliente,imei,installato) VALUES ('$comp','$imi',1)";
mysql_query($queryINS);
               }

else if ($numrows > 0) {
//never > 1 anyway
$volte=mysql_result($result,0,installato); 
      if($volte>0)       {
         $volte=$volte +1;
$queryUPDT="UPDATE licenze SET installato = $volte WHERE cliente = '$comp' AND  imei='$imi'";
mysql_query($queryUPDT);
                     }
                  }
$queryOUT="SELECT * FROM licenze WHERE cliente = '$comp' AND imei = '$imi'";
$output=mysql_query($queryOUT);   

$rows = array();
    while($r = mysql_fetch_assoc($output)) {
        $rows[] = $r;
    }
    print json_encode($rows);                  
?>
b4a
B4X:
Sub Process_Globals
Dim httpC As HttpClient
'  'BurgerLand' is a fantasy name of a client Company.Change the whole string to your address data
Dim indirizzo As String : indirizzo="http://www.xxxx.xxx/ctrllicence.php?company=BurgerLand&imei="
'[->Release(obfuscated)]
End Sub

Sub Globals
Dim p As PhoneId
Dim deviceID As String
   'Dim Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
   'if this file doesn't exists, you need to connect 
   If File.Exists(File.DirInternal,"checkLic.txt")= False Then
      deviceID = p.GetDeviceId
      httpC.Initialize("httpC")
      registerLicence
   Else
                           'if the controller file exists
      Msgbox("Not first app opening: no need to connect again","")
   End If
End If
'Activity.LoadLayout("1")
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub registerLicence
ProgressDialogShow("New installation. Licence validation ...")
Dim req As HttpRequest
req.InitializeGet(indirizzo&deviceID)
httpC.Execute(req,1)
End Sub

Sub httpC_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Dim result,rCompany As String
Dim ctrlTest As Int
Dim JSON As JSONParser
result = Response.GetString("UTF8")
'I remove '[' and ']' from the response received from my server -> array[{jsonObject}]
Dim tmp As String
Dim lungh As Int
tmp=result
lungh= tmp.Length
tmp= tmp.SubString2(1,lungh-1) 
result=tmp
'-----------------------------
JSON.Initialize(result)
Dim m As Map
m=JSON.NextObject
ctrlTest=m.Get("installato")
rCompany=m.Get("cliente")
ProgressDialogHide
   If ctrlTest>0 Then 'save controller file for not connecting in the future
      File.WriteString(File.DirInternal, "checkLic.txt","ok")
'(MsgBox only for demo)
  Msgbox("This Phone has "&ctrlTest&" new installation/s activated under "&rCompany&" Company.","")
   End If
Response.Release
End Sub

Sub httpC_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
ProgressDialogHide
   Log(Reason)
Msgbox("Web connection error. Try later again.","")'and no informations shown about hosting and php names
   If Response <> Null Then 'Null=if no web connection, so there's nothing to release.
      Response.Release
      Activity.Finish
   End If
Activity.Finish
End Sub
 

Attachments

  • first.jpg
    first.jpg
    19.3 KB · Views: 677
  • notFirst.jpg
    notFirst.jpg
    15.3 KB · Views: 594
  • second.jpg
    second.jpg
    20.6 KB · Views: 600
Last edited:

timo

Active Member
Licensed User
Longtime User
Thank you myriaddev.

And that's what you see quering your online MySql:
(you can use the same DBtable, but compile an apk for each company. If the control is made by the company self, use a dedicated table)
 

Attachments

  • licenze.jpg
    licenze.jpg
    49 KB · Views: 552
Last edited:
Top