3

I'm trying to track an APK install. When a user lands on the downloadpage (not the store), he is coming from a specific source. When user clicks on download, the APK will be installed. After it's installed, I need to map the install to the source the user was coming from before installing. Is there any good way to do this?

My plan so far: Save the user IP and screen resolutions on the download page to a database. After install, pass IP and screen resolution to the server and map with the row in the database. Is this a good way of doing this?

Hope you guys can help me.

John
  • 124
  • 8

2 Answers2

1

You just need to write a BroadcastReceiver for this which can receive the PACKAGE_ADDED and PACKAGE_INSTALL Intent:

InstallBroadcastReceiver.Class

public class InstallBroadcastReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
  String action = intent.getAction();
  if(action.equals(Intent.ACTION_PACKAGE_ADDED)
    ||action.equals(Intent.ACTION_PACKAGE_INSTALL)){
    notifyServerForApplicationInstall(context, intent);

    }
   }

  private void notifyServerForApplicationInstall(Context context,Intent intent){

    //send the data to your server here
  }
}

Register the receiver in AndroidManifest file

    <receiver
        android:name=".InstallBroadcastReceiver"
        android:exported="false" 
        <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <action android:name="android.intent.action.PACKAGE_INSTALL" />
            <data android:scheme="package" />
        </intent-filter>
    </receiver>

Don't forget to give this permissions in manifest :

<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
twlkyao
  • 14,302
  • 7
  • 27
  • 44
Piyush Agarwal
  • 25,608
  • 8
  • 98
  • 111
  • thanks so far! Is it a good way to map the source on ip and screensize? Or are there any better ways? – John Mar 03 '13 at 21:47
  • 1
    First, if the receiver is not exported, it cannot respond to those broadcasts. Second, that permission has nothing to do with these broadcasts AFAIK. Third, this has no relevance to the OP's question, as `android.intent.action.PACKAGE_ADDED` is not sent to the installed app, nor is it something that runs on the OP's Web server. – CommonsWare Mar 03 '13 at 21:54
  • 1
    for system app only ? – Tushar May 23 '13 at 19:39
  • No you can use it in third party app as well. – Piyush Agarwal May 24 '13 at 14:20
  • @pyus13 Can I detect the apk installing event when it is installing before it is installed. – twlkyao Jan 09 '14 at 15:09
  • no, the event only be fired when application get installed completely .Let me know your exact requirement, might be I can give you a workaround or any other solution. – Piyush Agarwal Jan 09 '14 at 15:48
  • @pyus13 I want to detect whether the app that I downloaded(from other apps) or get(copy from a computer) is the official one, if not I will not install it. Now I have a soluting to detect the file change on phone use `FileObserver` but it can't observer all the file changes, even I implement it recursively. – twlkyao Jan 11 '14 at 13:46
  • what do you mean by official apps(Downloaded form Google Play or Your Own apps)? Even if you detect file change how will you identify whether its an official app or not ? – Piyush Agarwal Jan 11 '14 at 15:43
0

I have prepared a BroadcastReceiver class :

public class newPackageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("DEBUG"," test for application install/uninstall");
    }

}

In the main activity, I first register a new receiver object, then instanciate button for application install.

public void onCreate(Bundle savedInstanceState) {
...
IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_PACKAGE_ADDED);
        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
        filter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
        filter.addAction(Intent.ACTION_PACKAGE_INSTALL);
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
        filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);

        receiver = new newPackageReceiver();
        registerReceiver(receiver, filter);
        ...

dlButton.setText(R.string.dl_button);
dlButton.setOnClickListener(new AppliDownloadOnClickListener(this ));   


@Override
public void onDestroy(){
     unregisterReceiver(receiver);
     super.onDestroy();
}
luttu android
  • 1,443
  • 16
  • 22