Skip to content

Instantly share code, notes, and snippets.

@liorkup
Last active February 13, 2020 08:08
Show Gist options
  • Save liorkup/93bcfb5a5ea1558e8fc9fefd22760392 to your computer and use it in GitHub Desktop.
Save liorkup/93bcfb5a5ea1558e8fc9fefd22760392 to your computer and use it in GitHub Desktop.
InstanceId solution for LadyM (License: https://gist.github.com/liorkup/019cec4f23edfe39a444571206ea2ae7)
//InstanceId solution
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {
// Do first run stuff here then set 'firstrun' as false
// using the following line to edit/commit prefs
prefs.edit().putBoolean("firstrun", false).commit();
@SuppressLint("StaticFieldLeak") AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
AdvertisingIdClient.Info idInfo = null;
try {
idInfo = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String advertId = null;
try{
storeIdInServer(mFirebaseAnalytics.getFirebaseInstanceId(), idInfo.getId());
}catch (NullPointerException e){
e.printStackTrace();
}
return advertId;
}
private void storeIdInServer(String firebaseInstanceId, String advertisingId) {
//todo
System.out.println( String.format("InstanceId: %s; AdvertisingId: %s", mFirebaseAnalytics.getFirebaseInstanceId(), advertisingId));
}
@Override
protected void onPostExecute(String advertId) {
Toast.makeText(getApplicationContext(), advertId, Toast.LENGTH_SHORT).show();
}
};
task.execute();
}
}
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment