Sunday, October 23, 2011

Working with gson parsing in android.

 Follow the steps in this link.you will be able to use the GSON library in android
http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html


Tuesday, September 20, 2011

How to access a Android database by using a command line.


When your start dealing with a database in your program, it is really important and useful to be able to access it directly, outside your program, to check what the program has just done, and to debug.

And it is important also on Android.

Here is how to do that :

1) Launch the emulator (or connect your real device to your PC ). I usually launch one of my program from Eclipse for this.
2) Launch a command prompt in the android tools directory.
3) type adb shell.
This will launch an unix shell on your emulator / connected device.
4) go to the directory where your database is :
cd data/data
here you have the list of all the applications on your device
Go in your application directory ( beware, Unix is case sensitive !! )
cd com.alocaly.LetterGame
and descend in your databases directory :
cd databases
Here you can find all your databases. In my case, there is only one ( now ) : SCORE_DB
5) Launch sqlite on the database you want to check / change :
sqlite3 SCORE_DB
From here, you can check what tables are present :
.tables
6) enter any SQL instruction you want :
select * from Score;

This is quite simple, but every time I need it, I don't know where to find it.

Wednesday, August 31, 2011

A basic implementation of SurfaceView

SurfaceView provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen. Access to the underlying surface is provided via the SurfaceHolder interface, which can be retrieved by calling getHolder().


package com.TestSurefaceView;
 
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
 
public class TestSurefaceView extends Activity {
 
MySurfaceView mySurfaceView;
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       mySurfaceView = new MySurfaceView(this);
       setContentView(mySurfaceView);
   }
   
   @Override
protected void onResume() {
 // TODO Auto-generated method stub
 super.onResume();
 mySurfaceView.onResumeMySurfaceView();
}
 
@Override
protected void onPause() {
 // TODO Auto-generated method stub
 super.onPause();
 mySurfaceView.onPauseMySurfaceView();
}
 
class MySurfaceView extends SurfaceView implements Runnable{
    
    Thread thread = null;
    SurfaceHolder surfaceHolder;
    volatile boolean running = false;
 
 public MySurfaceView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
  surfaceHolder = getHolder();
 }
 
 public void onResumeMySurfaceView(){
  running = true;
  thread = new Thread(this);
  thread.start();
 }
 
 public void onPauseMySurfaceView(){
  boolean retry = true;
  running = false;
  while(retry){
   try {
    thread.join();
    retry = false;
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
 
 @Override
 public void run() {
  // TODO Auto-generated method stub
  while(running){
   if(surfaceHolder.getSurface().isValid()){
    Canvas canvas = surfaceHolder.lockCanvas();
    //... actual drawing on canvas
    
    surfaceHolder.unlockCanvasAndPost(canvas);
   }
  }
 }
    
   }
}

Capture Screen, using View.getDrawingCache()

View screen = (View)findViewById(R.id.screen);
 
screen.setDrawingCacheEnabled(true);
Bitmap bmScreen = screen.getDrawingCache();

how to access sdcard in android Devolpment?

In this example I am accessing AudioRecorder Folder in sdcard.
String filepath = Environment.getExternalStorageDirectory().getPath();
        file = new File(filepath,"AudioRecorder");

        if (!file.exists())
        {
            file.mkdirs();
        }
 String[] a = file.list(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String filename)

            {
                return (filename.endsWith(".mp4") || filename.endsWith(".3gp")||filename.endsWith(".mp3"));

            }
        });

        for (String string : a)

        {

            a1.add(string);

        }