-1
  • I am working on app which needs to executes a web-service at app install and only once.
  • Currently i used Shared Preferences for this, But if user clear app data then it lost Shared Preferences value and my code detracts that app is newly created and my code executes web-service further.
  • So i need solution which broadcast event of my own app install.
  • I also create broadcast for that but broadcasts only when other app install.

This code i used...

For Check Status

private void checkAppStatus() {
    boolean isOpen = AppMethod.getBooleanPreference(MainActivity.this, AppConstant.PREF_IS_OPEN);
    if (!isOpen) {
        executeWS();
    }
}

Broadcast

public class AppInfoReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Log.e("My App", "Install");
  }
}

Manifest

<receiver android:name=".receiver.AppInfoReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_INSTALL" />
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <action android:name="android.intent.action.PACKAGE_REMOVED" />

            <data android:scheme="package" />
        </intent-filter>
</receiver>
Tecksky Android
  • 133
  • 3
  • 14
  • for an app in order to receive a Broadcast it should be launched or running in background, an app while in installation phase is not able to receive any Broadcasts. you can send `android-id` or `device-id` as param to the web-service to guarantee the uniqueness you are looking for and keep using the `Shared Preferences` approach i think it's best fit for this case – Yazan Sep 18 '16 at 07:13
  • You can done by INSTALL_REFERAL. It will run only when app will install in device. check it. – D.J Sep 19 '16 at 05:39

2 Answers2

0

If you have a server, you can save status in your server and get the status before execute task

yao liu
  • 121
  • 8
0

You have following options:

  1. You can create a file in hidden directory on external memory with needed information.
  2. You can create and use your web service for those needs.

Your code doesn't work because your application not installed at that time when broadcast created and because in android your application can receive broadcasts only if it was launched at least one time by user.

As I understood, first option doesn't fit your needs too, cause there is some probability that external memory would be erased or replaced. Android devices doesn't provide any kind of memory that 100% would be persistent in time, and if you really need information about installation of your application you should run your server that receives android device id and sends back to your application information about rather it is first installation or re installation. However I suppose that you should assume clearing app data as application reinstall and run your web service one more time. Simplest solution: just pass to your service android device id and do your things if it is new id or not if it is already done.

akaish
  • 68
  • 8