Test Script

Saturday, April 27, 2013

Fetching User's Email ID

Hi guys in this tutorial I am going to be showing you how to fetch the user's email Id which has been tied to the users device. This is a very short & simple tutorial so without wasting further time lets get to work :

================================================================
MainActivity.java
================================================================


package com.example.testemail;



import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String emailId = getEmailId();
        Toast.makeText(getApplicationContext(), emailId, 500).show();

}

   public String getEmailId(){

      Account[] accounts=AccountManager.get(this).getAccountsByType("com.google");
    
   
    for(Account account: accounts)
    {
        String emailId=account.toString();
        Log.d("List of  email id's of user", emailId);

    }

     String myEmailid=accounts[0].name;

    return myEmailid;
   }

}

================================================================

You also need a permission to access the Accounts 

<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 

So add the above permission to your Manifest file.

================================================================




So, let me quickly explain what i did above :
1) I created a method which fetches the User's email ID
2) Here we get a Array of Accounts using the AccountManager, notice "getAccountsByType("com.google");" 
that is used to to get all Gmail Accounts configured in the users device

3) "accounts[0].name"  gives me the Email Id of the first Account of the user, which is most likely to be the users actual Gmail Id.

4) Finally added a permission of GET_ACCOUNTS required to access the accounts.

Thats all there is, this is used to fetch user's gmail Id programmatically.


================================================================

Note : Try this on a actual device rather than a emulator because the emulator has no Email Id by default .

If this post helped you, please do drop a comment & i will be happy !

As always feel  free to comment or fire any queries to me ! 



Monday, April 15, 2013

Google Analytics Integration



Hi, so as  promised I am  starting to write a few tutorials !

In this tutorial I am going to teach you how to integrate the latest version of Google Analytics (v2) into your Android app.

Before i begin, let me tell you why your app should have Google Analytics in it.

Google analytics is a great tool for generating reports on your app's performance after users have started using it. Some features of Google Analytics are :


  • Number of active users using their app
  • Demographics of users
  • Adoption and usage of specific features
  • In-app purchases and transactions
  • Number and type of application crashes




So let 's get started on integrating these features in your application .

I will be using two classes to show you how to get this working
a) Main.java
b) Second.java
================================================================

Pre Requisites needed  before we integrate Google Analytics :

1. Create a new Google Analytics Account -->  Create Account

2. Add a new mobile app in your account(This is the app which you want to track)

3. After you add a app in your account,google provides you a tracking Id unique for that app. Note down the tracking ID , it is usually in  UA-XXXX-Y format

4. Download the Google Analytics jar file --->Jar Download Link


==================================================================

Update Manifest File :


<uses-permission android:name="android.permission.INTERNET" /> 

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>



===================================================================

Create a new analytics.xml File :

Create a new xml file in the res/values directory.

XML File

Replace UA-XXXX-Y with the tracking Id you received.

Here we set ga_autoActivityTracking & ga_reportUncaughtExceptions true so that you can monitor your Activities & Exceptions.

Also i have added the following lines so as to give my screens/activities a desired name when it shows up in reports .

 name="com.example.testapp.Main">Main
 name="com.example.testapp.Second">Second


Here name is the complete name of the activity including the package. You have to add all the screens you want to monitor & name them as per desire.


==============================================================
Main.java
==============================================================



package com.example.testapp;

import com.google.analytics.tracking.android.EasyTracker;

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

public class MainActivity extends Activity {
Button b1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1=(Button) findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
    }


    @Override
    public void onStart() {
      super.onStart();
       // The rest of your onStart() code.
      EasyTracker.getInstance().activityStart(this); // Add this method.
    }
    
    @Override
    public void onStop() {
      super.onStop();
      // The rest of your onStop() code.
      EasyTracker.getInstance().activityStop(this); // Add this method.
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

=================================================================

So in the above class , i did the following things :

1) Imported Google Analytics package 
2) Added the method  EasyTracker.getInstance().activityStart(this) to start tracking this activity/screen.
3) Added the method  EasyTracker.getInstance().activityStop(this) to stop tracking this activity/screen.
4) Also there is a button which on click opens up Second.java


==============================================================
Second.java
==============================================================

package com.example.testapp;

import com.google.analytics.tracking.android.EasyTracker;

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

public class Second extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sec);
}
  @Override
    public void onStart() {
      super.onStart();
      // The rest of your onStart() code.
      EasyTracker.getInstance().activityStart(this); // Add this method.
    }
   
    @Override
    public void onStop() {
      super.onStop();
      // The rest of your onStop() code.
      EasyTracker.getInstance().activityStop(this); // Add this method.
    }
}

=================================================================

In this class as you can see,i did the exact same things as in Main.java. So you will need to override the onStart & onStop methods of all activities you want to monitor .

That's it ! Pretty simple right? Now once you have done this you can go the dashboard of your Google Analytics account & track the reports.

Note : Google Analytics also provides a feature to measure and track events,i will try to make a new post about it soon. I will be showing how you can track button clicks and other events.

If you need any help or have any request feel free to drop me a comment below !

Monday, April 8, 2013

More Tutorials

So finally i have some time off, so i plan to start a few tutorials on Android ! Currently there is  nothing in particular in my  mind,so if anyone has any requests/suggestions plz let me know & i will try to make a tutorial on the same !

I will eventually be doing a lot of tutorials right from scratch with very clear explanation so that everyone can understand it. I plan to break up my tutorials into small modules. Each module will perform a particular task. So by the end you can have code for almost all functionality within Android.

I find that there is no one place on internet where there are a lot of tutorials right from simplest things so i plan to do that and create a place where a person can find everything on one site itself.

Let me know your views or comments on the same

Also dont forget to see my previous work here :

www.myandroidportfolio.blogspot.com
UA-42774700-1