Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-24440] Android: get Application context

GitHub Issuen/a
TypeNew Feature
PriorityNone
StatusClosed
ResolutionDuplicate
Resolution Date2017-03-02T19:01:56.000+0000
Affected Version/sRelease 6.0.1
Fix Version/sn/a
ComponentsAndroid
Labelsn/a
Reporter Ricardo Ramirez
AssigneeEric Merriman
Created2017-02-28T22:20:27.000+0000
Updated2017-03-02T19:01:56.000+0000

Description

Issue Description

getapplicationcontext() method is present in android native code to get application context. This is the code that needs to be executed in app.js
var localytics = require('com.localytics.android.Localytics');
localytics.autoIntegrate(application context);
Customer is not able to get current application context. He tried lot of things.
var context = require("android.context.Context");
var appContext = context.getApplicationContext();
This one throwing error stating could not find getapplicationContext method. https://developer.android.com/reference/android/content/Context.html#getApplicationContext()

Comments

  1. Hans Knöchel 2017-03-01

    The above code only requires the class and tries to call a method on that, which will never work, because it isn't an instance, just a class. [~cwilliams] may be able to help where to call the getApplicationContext from. Maybe this *could* work:
       var activity = new Activity(Titanium.App.Android.getTopActivity());
       var appContext = activity.getApplicationContext();
       
    Because the android.app.Activity class indirectly inherits from the android.content.Context class. Otherwise, and I'm not sure if that's possible so far, you could try to require org.appcelerator.titanium.TiApplication (the internal classes) to call TiApplication.getInstance().getApplicationContext(). But I think those classes are not generated so far.
  2. Jan Vennemann 2017-03-02

    [~rramirez], please also check my comments in TIMOB-24420 which is exactly about the same issue.
  3. Ricardo Ramirez 2017-03-02

    Thanks ! [~nmishra] please take a look
  4. Ricardo Ramirez 2017-03-02

    Be my guest.

JSON Source