Friday, 15 June 2012

Accessing Android Default Browser To Access A site Through Java Code


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="www.refisoft.blogspot.in" />
 
</LinearLayout>

package com.refisoft;

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

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class RefisoftActivity extends Activity implements
  OnClickListener {
 Button button;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  button = (Button) findViewById(R.id.button1);
  button.setOnClickListener(this);
 }

 public void onClick(View v) {
  // TODO Auto-generated method stub
  if (v.getId() == R.id.button1) {
    Intent tobrowser = 
                 new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.refisoft.blogspot.in"));
     startActivity(tobrowser);

  }

 }

}





AndroidManifest.xml Add Like this For Internet permission


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



Thanks All...........................................................................







Saturday, 2 June 2012

Android Sqlite CRUD operations with Login


Play with this codes....


package com.dbapp;
import android.app.Activity;
import android.os.Bundle;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.Locale;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.ContentValues;
import android.content.Intent;

public class DbAppActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
SQLiteDatabase db;
Button insert,select,delete,update;

EditText editText1,editText2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        insert=(Button)findViewById(R.id.insert);
        select=(Button)findViewById(R.id.select);
        delete=(Button)findViewById(R.id.delete);
        update=(Button)findViewById(R.id.update);
        editText1=(EditText)findViewById(R.id.editText1);
        editText2=(EditText)findViewById(R.id.editText2);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
       
        try
        {
        db = openOrCreateDatabase("baabtra.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
        db.setVersion(1);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);

    final String tbl_userdetail="CREATE TABLE userdetail("
+ "username INTEGER PRIMARY KEY AUTOINCREMENT,"

+ "
place TEXT);";  
db.execSQL(
tbl_userdetail);


        }catch (Exception e) {

        Toast.makeText(this.getBaseContext(),e.getMessage(),
                    Toast.LENGTH_SHORT).show();

}
    }
   
public void onClick(View v)
{
switch(v.getId())
     {
       case R.id.insert:
           {
                    try {
ContentValues values = new ContentValues();

       values.put("username", editText1.getText().toString());
       values.put("place", editText2.getText().toString());
       db.insert("userdetails", null, values);
       Toast.makeText(this.getBaseContext(),"inserted",
               Toast.LENGTH_SHORT).show();

   } catch (Exception e) {

Toast.makeText(this.getBaseContext(),e.getMessage(),
                   Toast.LENGTH_SHORT).show();

   }
           break;
           }
       case R.id.select:
           {
            String place;
                    try {
                 
                    Cursor cursor = db.rawQuery("SELECT * FROM userdetails WHERE place like'%"+editText1.getText()+"%'", null);

                   if (cursor != null) {
                       if (cursor.moveToFirst()){
                           do {
                               place = cursor.getString(cursor.getColumnIndex("username"));
                              Toast.makeText(this.getBaseContext(),place,
                   Toast.LENGTH_SHORT).show();
                           }while (cursor.moveToNext());
                       }
                   } else {
                       place = "oops";
                   }
   } catch (Exception e) {

Toast.makeText(this.getBaseContext(),e.getMessage(),
                   Toast.LENGTH_SHORT).show();

   }
 
           break;
           }
       case R.id.delete:
           {
            String place;
                    try {
                    this.db.delete("userdetails", "place="+editText1.getText() , null);
                    Toast.makeText(this.getBaseContext(),"deleted",
                   Toast.LENGTH_SHORT).show();
   } catch (Exception e) {

Toast.makeText(this.getBaseContext(),e.getMessage(),
                   Toast.LENGTH_SHORT).show();

   }
 
           break;
           }
       case R.id.update:
           {
            Intent intent = new Intent(DbAppActivity.this,loginpage.class);
            startActivity(intent);
            break;
           
           }
     
     
     
}

}

}

Android Mp3 Player


This Example Helps you to create a simple Mp3 player.Which reades a file from sd card and play it.
try it by using a listview displays list of mp3 songs and select one mp3.which plays on click event of a play button.

package com.Mp3Player;

import java.io.File;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class Mp3PlayerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
MediaPlayer mediaplayer = new MediaPlayer();
mediaplayer.setDataSource("/sdcard/Music/refisoftmp3song1.mp3");
mediaplayer.prepare();
mediaplayer.start();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_SHORT).show();
}
}
}



populate the mp3 file from sd card using this code.


File f = new File("/sdcard/Music");
 if (f.isDirectory()) {
String files[] = f.list();
 for (int i = 0; i < files.length; i++) {
 Log.d("", files[i]);
}



Try it All the best



Friday, 1 June 2012

Android Sqlite Connection


1.This Code helps to create a sqlite databse

/*----------------------------open and create database----------------------------------*/

package com.sqliteexample;

import android.app.Activity;
import android.os.Bundle;
import android.database.sqlite.SQLiteDatabase;

public class SQLiteTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SQLiteDatabase db;
db = openOrCreateDatabase(
"RefisoftDb.db"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
}
}
/*--------------------------------------------------------------*/
2.open command prompt

C:\Android\android-sdk\platform-tools>adb devices
List of devices attached
emulator-5554 device

To view the database create we have to log into adb shell,for this in command prompt
C:\Android\android-sdk\platform-tools>adb -s emulator-5554 shell
# ls /data/data/com.sqliteexample/databases
RefisoftDb.db
# sqlite3 /data/data/example.SQLiteTest/databases/TestData.db

Android Sqlite Applications