1

// Google Analytics class

public class MyApplication extends AppApplication {
    public static final String TAG = MyApplication.class
            .getSimpleName();

    private static MyApplication mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;

        AnalyticsTrackers.initialize(this);
        AnalyticsTrackers.getInstance().get(AnalyticsTrackers.Target.APP);
    }

    public static synchronized MyApplication getInstance() {
        return mInstance;
    }

    public synchronized Tracker getGoogleAnalyticsTracker() {
        AnalyticsTrackers analyticsTrackers = AnalyticsTrackers.getInstance();
        return analyticsTrackers.get(AnalyticsTrackers.Target.APP);
    }

    /***
     * Tracking screen view
     *
     * @param screenName screen name to be displayed on GA dashboard
     */
    public void trackScreenView(String screenName) {
        Tracker t = getGoogleAnalyticsTracker();

        // Set screen name.
        t.setScreenName(screenName);

        // Send a screen view.
        t.send(new HitBuilders.ScreenViewBuilder().build());

        GoogleAnalytics.getInstance(this).dispatchLocalHits();
    }

   
    public void trackException(Exception e) {
        if (e != null) {
            Tracker t = getGoogleAnalyticsTracker();

            t.send(new HitBuilders.ExceptionBuilder()
                    .setDescription(
                            new StandardExceptionParser(this, null)
                                    .getDescription(Thread.currentThread().getName(), e))
                    .setFatal(false)
                    .build()
            );
        }
    }

    
    public void trackEvent(String category, String action, String label) {
        Tracker t = getGoogleAnalyticsTracker();

        // Build and send an Event.
        t.send(new HitBuilders.EventBuilder().setCategory(category).setAction(action).setLabel(label).build());
    }

}
// my application class which extends MultiDexApplication 



public class AppApplication extends MultiDexApplication {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }



 @Override
 public void onCreate() {
  super.onCreate();
  
  

  
 }
 public static void checkrating(final Activity activity){
       
   new Handler().postDelayed(new Runnable() {

    @Override
       public void run() {
        checkratingdialog(activity);
       }
      }, 10000 /*300000*/);
 }
 
 public static void checkratingdialog(Activity activity) {
  
  try {
   if(activity==null){
    Log.v("activity",   "null");
   }
   Calendar cal = Calendar.getInstance();
      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
      String currentdate = sdf.format(cal.getTime());
      Log.v("Current date", currentdate);
      
   SharedPreferences sharedPreferences = activity.getSharedPreferences(
     "MY_SHARED_PREF", MODE_PRIVATE);
   String shareddate  = sharedPreferences.getString("PREVIOUS_RATING_DATE", "");
   
   Log.v("Shared Date", shareddate + "");



            if(shareddate.equals("")){
    Log.v("Shared Date",  "Shared blank");
    showWaitingnooftickitsDialog(activity);

                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("PREVIOUS_RATING_DATE", currentdate);
                editor.commit();


   }else{
    
    int dayes =  get_count_of_days(currentdate , shareddate); 
    Log.v("dayes",  dayes+"");
    /*int dayescheck =  get_count_of_days(currentdate , "20/05/2016"); 
    Log.v("dayescheck",  dayescheck + "");*/
    if(dayes>=30){
     showWaitingnooftickitsDialog(activity);

                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("PREVIOUS_RATING_DATE", currentdate);
                    editor.commit();
    }
   }
   

      
      
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
  
 }

 static Dialog ratingdialog; 
   
}

I've an Application class which extends MultiDexApplication and another which extends application. I want to register both class name in the application tag of manifest. I searched for it and found that we can use multi-level inheritance as shown here. But how to register both class when one is plain Application class and another is MultiDexApplication class?

Community
  • 1
  • 1
Ushasi
  • 13
  • 3
  • add your code what you want to do all can be happen in single class. – Sohail Zahid Aug 22 '16 at 05:39
  • write your google analytics code in AppApplication class which extends MultiDexApplication class cause it is directly extending application class and multilevel inheritance is being used there. – Jay Shah Aug 22 '16 at 05:58
  • Will there be any problem if I use both these classes as it is and add the name `MyApplication` to Manifest? @JayShah – Ushasi Aug 22 '16 at 06:14
  • 1
    It will not possible.We can not define two application level classes – Jay Shah Aug 22 '16 at 07:15
  • Thanks for the suggestion. I implemented the analytics code in AppApplication class and now its working fine. @Jay Shah – Ushasi Aug 22 '16 at 10:20
  • @Ushasi If you found my comment useful then please upovote my comment – Jay Shah Aug 22 '16 at 10:39

1 Answers1

1

You can add below method in your existing application class to support Multidex files -

protected void attachBaseContext(Context base) {
  super.attachBaseContext(base);
  MultiDex.install(this);
}
Passiondroid
  • 1,573
  • 1
  • 16
  • 28
  • the class which extends MultiDexApplication has some extra code so I need to use both the classes. @Passiondroid – Ushasi Aug 22 '16 at 05:50