Tag Archives: Industrial Training

Android Global Variables/Application Context Variable

In this example we are going to learn to define variables in application context and we can use this variables as a Global variable. We can set values in this variables  and get values on any activity / broadcast recieiver / service in application context(environment).

The challenge is how to save values across several Activities and all parts of your application context. We can achieve this by creating static variable but it is not a good way as it will causes memory leaks. The solution for this in Android is to associate your variable with the Application context.

Every Application has a context, and Android guarantees that Application context will exist as a instance across your application.

The way to do this is to create a class and extends with android.app.Application, and specify your class in the application tag in your AndroidManifest.xml file. Android will create an instance of that class and make it available for your entire application context. We can get object of our class on any activity / broadcast reciever / service in application context(environment) by Context.getApplicationContext() method.

Now lets create a project to understand variables under Application Context:-

Create a class named AuthenticationDetails.java and extent it with ‘Application’ our custom class should be subclass of android.app.Application class. We will use this class as global class for your Application environment(Conext).

AuthenticationDetails.java

package com.coreprogrammers.model;

import android.app.Application;

public class AuthenticationDetails extends Application
{
	private String id;
	private String name;
	private String company;
	private String username;
	private String password;
	private String s3_key;
	private String s3_secret;
	private String api_key;
	private String license;
	private String timezone;
	private String tied_to;
	private String app;
	private String paypal;
	private String cron;
	private String cron_ares;
	private String send_rate;
	private String language;
	private String cron_csv;
	private String login_in;

	public void setid(String strid) 
	{
		this.id = strid;
	}

	public String getid() 
	{
		return id;
	}

	public void setname(String strname) 
	{
		this.name = strname;
	}

	public String getname() 
	{
		return name;
	}

	public void setcompany(String strcompany) 
	{
		this.company = strcompany;
	}

	public String getcompany() 
	{
		return company;
	}

	public void setusername(String strusername) 
	{
		this.username = strusername;
	}

	public String getusername() 
	{
		return username;
	}

	public void setpassword(String strpassword) 
	{
		this.password = strpassword;
	}

	public String getpassword() 
	{
		return password;
	}

	public void sets3_key(String strs3_key) 
	{
		this.s3_key = strs3_key;
	}

	public String gets3_key() 
	{
		return s3_key;
	}

	public void sets3_secret(String strs3_secret) 
	{
		this.s3_secret = strs3_secret;
	}

	public String gets3_secret() 
	{
		return s3_secret;
	}

	public void setapi_key(String strapi_key) 
	{
		this.api_key = strapi_key;
	}

	public String getapi_key() 
	{
		return api_key;
	}

	public void setlicense(String strlicense) 
	{
		this.license = strlicense;
	}

	public String getlicense() 
	{
		return license;
	}

	public void settimezone(String strtimezone) 
	{
		this.timezone = strtimezone;
	}

	public String gettimezone() 
	{
		return timezone;
	}

	public void settied_to(String strtied_to) 
	{
		this.tied_to = strtied_to;
	}

	public String gettied_to() 
	{
		return tied_to;
	}

	public void setapp(String strapp) 
	{
		this.app = strapp;
	}

	public String getapp() 
	{
		return app;
	}

	public void setpaypal(String strpaypal) 
	{
		this.paypal = strpaypal;
	}

	public String getpaypal() 
	{
		return paypal;
	}

	public void setcron(String strcron) 
	{
		this.cron = strcron;
	}

	public String getcron() 
	{
		return cron;
	}

	public void setcron_ares(String strcron_ares) 
	{
		this.cron_ares = strcron_ares;
	}

	public String getcron_ares() 
	{
		return cron_ares;
	}

	public void setsend_rate(String strsend_rate) 
	{
		this.send_rate = strsend_rate;
	}

	public String getsend_rate() 
	{
		return send_rate;
	}

	public void setlanguage(String strlanguage) 
	{
		this.language = strlanguage;
	}

	public String getlanguage() 
	{
		return language;
	}

	public void setcron_csv(String strcron_csv) 
	{
		this.cron_csv = strcron_csv;
	}

	public String getcron_csv() 
	{
		return cron_csv;
	}

	public void setlogin_in(String strlogin_in) 
	{
		this.login_in = strlogin_in;
	}

	public String getlogin_in() 
	{
		return login_in;
	}	
}

 

Now We are going to create  AndroidManifest.xml.

In AndroidManifest file we will Assign AuthenticationDetails.java in application tag, see this line in application tag android:name=”com.coreprogrammers.model.AuthenticationDetails” . After assign we can get AuthenticationDetails.java instance on any activity / broadcast reciever / service in application context.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.coreprogrammers.model"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" />

    <application
        android:name="com.coreprogrammers.model.AuthenticationDetails"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.coreprogrammers.model.FirstScreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

        <activity android:name="com.coreprogrammers.model.SecondScreen" ></activity>  
        <activity android:name="com.coreprogrammers.model.ThirdScreen" ></activity>
    </application>

</manifest>

Now we will show how to use the global variables across three activities

1) FirstScreen.java

getApplicationContext() method of Context will give AuthenticationDetails.java instance and set id/name values and maintain state of AuthenticationDetails.java instance.

package com.coreprogrammers.model;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;

public class FirstScreen extends Activity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstscreen);

        final Button secondBtn = (Button) findViewById(R.id.second);

        // Calling Application class (see application tag in AndroidManifest.xml)
        final AuthenticationDetails _objAuthenticationDetails = (AuthenticationDetails) getApplicationContext();

        //Set name and id in global/application context
        _objAuthenticationDetails.setname("Coreprogrammers");
        _objAuthenticationDetails.setid("Rocks");

        secondBtn.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
                Intent i = new Intent(getBaseContext(), SecondScreen.class);
                startActivity(i);
            }
        }); 
    }   
}

 

2) SecondScreen.java

We have to Call getApplicationContext() method of Context and get AuthenticationDetails.java instance , By AuthenticationDetails.java instance we can get set values for name/id variables.

package com.coreprogrammers.model;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SecondScreen extends Activity 
{

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondscreen); 

        TextView showGlobal = (TextView) findViewById(R.id.showGlobal);
        final Button thirdBtn = (Button) findViewById(R.id.third);

        // Calling Application class (see application tag in AndroidManifest.xml)
        final AuthenticationDetails _objAuthenticationDetails = (AuthenticationDetails) getApplicationContext();

        // Get name and id from global/application context
        final String name  = _objAuthenticationDetails.getname();
        final String id = _objAuthenticationDetails.getid();

        String showString = "Name= : "+ name + "ID : "+ id;

        // Show name/email values in TextView
        showGlobal.setText(showString);

        thirdBtn.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v) 
            {
                Intent i = new Intent(getBaseContext(), ThirdScreen.class);
                startActivity(i);
            }
        });

    } 

    @Override
    protected void onDestroy() 
    {
        super.onDestroy();
    }
}

 

3) ThirdScreen.java

Now we will call getApplicationContext() method of Context and get AuthenticationDetails.java instance , By AuthenticationDetails.java instance we can get and set values for name/id variables.

package com.coreprogrammers.model;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ThirdScreen extends Activity 
{

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thirdscreen); 

        TextView showGlobal = (TextView) findViewById(R.id.showGlobal);

        // Calling Application class (see application tag in AndroidManifest.xml)
        final AuthenticationDetails _objAuthenticationDetails = (AuthenticationDetails) getApplicationContext();

        // Get name and email from global/application context
        final String name  = _objAuthenticationDetails.getname();
        final String id = _objAuthenticationDetails.getid();

        String showString = "Name= "+ name +" "+ "ID= "+ id;

        // Show name/id values in TextView
        showGlobal.setText(showString);

    } 
}

Happy Programming :-)

 

Home

LOGO1

We Develop Programmers, We Develop Professionals

[Training, Outsourcing, Development]

Internship for computer science students

Office: +919784909487

Email: Softwareengineer.developer@gmail.com

Office Address: 1-FA-11, Hiran Magri, Sector-5, Gayatri-Nagar, Udaipur, 313002, [Rajasthan]

SERVICES:
Campus Training
Corporate Training

 

Industrial Training:
45 Days Training
6 Weeks Training
4 Months Training
6 Months Training
8 Months Training

Shape your carrier with CoreProgrammers IT courses

CoreProgrammers is a leading name in the computer training industry. We are known for churning out globally competent professionals who are miles ahead of others in skill set, knowledge and ability to deliver. We have a wide bouquet of IT courses that are industry centric and job oriented. These courses have enabled many freshers find lucrative jobs and the experienced professionals advance in their career. Most of our students have proved to be productive in their endeavors from the very beginning with little or no training.

CoreProgrammers knows when students approach an institute they expect more value than the books on technology can offer. Hence our IT courses are designed and delivered by consultants working in the specific technologies. The students get the triple benefit of in-depth understanding of the technology, its practical application in real time scenarios and opportunity to work in live projects. Time and again we arrange for guest lectures by industry tech experts to give insights on various trends and guidance on real life project.

We are one of the fastest growing software training, software solution, technical consultancy and knowledge outsourcing company situated in India with offices at UDAIPUR and JAIPUR. Our core areas are Mobile Development & training, client server technology, and web based applications. CoreProgrammers focus is on defining new modes of knowledge sharing systems to create pool of experts and skilled resources in the field of IT and cutting edge technologies.

CoreProgrammers courses are for individuals as well as for corporates. We also undertake customization of the courses to training milestones of the organizations. We are also in software development and instructor led campus workshops. We make them industry ready and offers would automatically knock their door.

CoreProgrammers Known For its Excellence in Software Education. We arrange workshop and career oriented courses like; Java: Training in Java, Online training in Java, Android Development,
iPhone Development,.NET: .NET Training Classes, .NET framework, Asp.NET Training Course, Asp.NET Ajax, C# , Embedded: Embedded Systems, VC++, Sql Reference, Software Quality Testing: SQT Training, Testing Web Applications, Automaton Training along with Testing Techniques, .NET MVC, PHP(Core PHP, Magneto, Cake PHP, Drupal, WordPress) , Blackberry, Windows Phone Apps, Windows 8 Store Apps, Flex, Java, Java Net Beans, Core Java, C, C++, Assembly Language, Embedded Programming., UBUNTU Mob. Application, windows applications, jQuery, JavaScript, MS Sql, Oracle, My Sql, HTML, HTML5.

OUR VISION

To help our Students & customers To achieve their objectives by providing innovative, best-in- class consulting, IT solutions, services & Training. Make it a joy for all to work with us.

 

OUR MISSION

Our mission is to achieve excellence in the Education, Training, Software products &  services we offer  in the methods we employ and in the results we produce.

 

OUR PHILOSOPHY

The culture at CoreProgrammers revolves around teamwork, knowledge sharing, innovation and enrichment. It’s an environment where open communication is encouraged and the spirit of excellence is rewarded.

Software Development :-

  1.  Android/ Blackberry/ iPhone/ Windows Phone Mobile Application Development .
  2.  Asp.NET/.NET MVC/ PHP(Core PHP, Magneto, Cake PHP, Drupal, WordPress) WebSiteDevelopment.
  3.  CRM in .Net web/windows application
  4.  Mac Application Development.

Computer Science (Academic Courses)

Classes For All practical Subjects Like

  • ➢  C,C++
  • ➢  Data structure Through C Language
  • ➢  Computer System architecture
  • ➢  Java
  • ➢  Java Net Beans
  • ➢  .NET,ASP.NET
  • ➢  Core Php
  • ➢  Artificial intelligence
  • ➢  Android
  • ➢  iPhone
  • ➢  Windows 8 Store/Phone apps

For PGDCA, B-Tech, B.E, BCA, MCA, M.Sc [CS], M.Sc [IT], M Tech Of Universities, IGNOU, O-Level & A-Level.

  • ➢  C.B.S.E Computer Science XI & XII .Rs 6500 /- For Complete Course for any of the above subjects.

 

 OUR Professional Courses

Development (Professional) / 45 days – 8 Months Industrial Training

➢ Android

➢ Asp.NET

➢ .NET MVC

➢ PHP(Core PHP, Magneto, Cake PHP, Drupal, WordPress)

➢ Blackberry

➢ iPhone

➢ Windows Phone Apps

➢ Windows 8 Store Apps

➢ Flex

➢ Java

➢ Java Net Beans

➢ Core Java

➢ C, C++,

➢ Assembly Language/ Embedded Programming.

➢ UBUNTU Mob. Application

➢ windows applications

➢ jQuery/ JavaScript

➢ MS Sql/ Oracle/ My Sql

➢ HTML/ HTML5

➢ Perl

➢ Python

➢ Ruby.

Basic Professional Course  (Duration – 6 months) (Fundamentals, Controls with examples, a minor project, Training Certificate)

Advance Professional Course –(Duration – 8 months) (Fundamentals, Controls with examples, a minor project, major Project, Training Certificate )

NOTE :-

  1. In Mobile Programming Practical Classes applications will be tested on real mobile devices.
  2.  Life Time Technical assistance will be provided to the students of Advance Professional Course .
  3.  Free Access to E-Library only For Registered Students.
WebSites:- FaceBook Links:-
www.coreprogrammers.com Profile_Link :- https://www.facebook.com/coreprogrammers
www.fundoocode.net Page Link 1:- https://www.facebook.com/fundoocode.net
www.coreprogrammers.wordpress.com Page Link 2:- https://www.facebook.com/automobileEncyclopedia
www.fundoocode.wordpress.com

Google+

Skip to toolbar