Tuesday, July 17, 2012

Send SMS to more than one device



 Click Here to Download source code

Package Name   :  selva.sms

Project Name     :   SMS2

Version              :    1.5 ( Supports 1.5 and above versions)

  
main.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/btnSendSMS"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send SMS" />
</LinearLayout>




 SMS2Activity.java



package selva.sms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SMS2Activity extends Activity {
Button btnSendSMS;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
    Intent i = new
            Intent(android.content.Intent.ACTION_VIEW);
            i.putExtra("address", "5556; 5558; 5560");
            i.putExtra("sms_body", "Hello my friends!");
            i.setType("vnd.android-dir/mms-sms");
            startActivity(i);
}
});
}


}




 AndroidManifest.xml


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

    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SMS2Activity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



OUTPUT:





 Click Here to Download source code


Monday, July 16, 2012

Send SMS and Receive Delivery Report in Android



Click Here To Download Source Code

Package Name   :  selva.sms

Project Name     :  SMS1

Version              :  1.5 ( Supports 1.5 and above versions)

main.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/btnSendSMS"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send SMS" />
</LinearLayout>




SMS1Activity.java


package selva.sms;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class SMS1Activity extends Activity
{
Button btnSendSMS;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
sendSMS("5556", "Hi You got a message!");
}
});
}
//---sends an SMS message to another device---

private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
}



 AndroidManifest.xml


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

    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SMS1Activity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



OUTPUT: 






Click Here To Download Source Code


How To Send SMS in Android



Click Here to download Source Code

Package Name   :  selva.sms

Project Name     :  SMS

Version              :  1.5 ( Supports 1.5 and above versions)



main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/btnSendSMS"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send SMS" />
</LinearLayout>



 SMSActivity.java



package selva.sms;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
public class SMSActivity extends Activity {
Button btnSendSMS;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
sendSMS("5556", "Hi You got a message!");
}
});
}
//---sends an SMS message to another device---

private void sendSMS(String phoneNumber, String message)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}

}


AndroidManifest.xml

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

    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SMSActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


 OUTPUT:








click Send SMS button in 5554 emulator




 




















You Will get message on top of the emulator 5556













Click Here to download Source Code



To Get Your phone contact and display in list



Click Here To download Source code

Package Name  :  selva.contact

Project Name    Contact

Version             :  1.5 ( Supports 1.5 and above versions )

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:stackFromBottom="false"
    android:transcriptMode="normal"/>
    <TextView
    android:id="@+id/contactName"
    android:textStyle="bold"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
    <TextView
    android:id="@+id/contactID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>




ContactActivity.java



package selva.contact;

import android.os.Bundle;
import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.widget.SimpleCursorAdapter;
public class ContactActivity extends ListActivity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Uri allContacts = Uri.parse("content://contacts/people");
        Cursor c = managedQuery(allContacts, null, null, null, null);
        String[] columns = new String[] { ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts._ID };
        int[] views = new int[] {R.id.contactName, R.id.contactID};
        SimpleCursorAdapter adapter =new SimpleCursorAdapter(this, R.layout.main, c, columns, views);
        this.setListAdapter(adapter);
}
}




AndroidManifest.xml


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

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.READ_CONTACTS"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".ContactActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



OUTPUT:

click call button in emulator











click contacts tab
















































select New contact














































Fill the Details in New contact Form













































Click Done Button














































Contacts are appeared in screen







































Now run the program































Click Here To download Source code


Creating Text File and using in Android



Click Here to download source code

Package Name   :   selva.file

Project Name     :   File2

Version              :   1.5 ( Support 1.5 and above versions)

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Please enter some text"/>
    <EditText
    android:id="@+id/txtText2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
    <Button
    android:id="@+id/btnLoad"
    android:text="Load"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>



(create raw folder under res) 
res-->raw-->rightclick-->new-->file-->textfile 

















 
textfile.txt

hi.. this is sample file content..


  File2Activity.java


 package selva.file;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;


public class File2Activity extends Activity
{
    /** Called when the activity is first created. */
    private EditText textBox;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
       
    textBox = (EditText) findViewById(R.id.txtText2);
    Button loadBtn = (Button) findViewById(R.id.btnLoad);
   
loadBtn.setOnClickListener(new View.OnClickListener()
{
    public void onClick(View v)
    {
        InputStream  is=File2Activity.this.getResources().openRawResource(R.raw.textfile);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String str=null;
        try {
        while ((str = br.readLine()) != null)
        {
           
            textBox.setText(str);   
        }
   
        is.close();
        br.close();
        }
        catch (IOException e)
        {
        e.printStackTrace();
        }
    }
}); 
}
}


OUTPUT:


























 click Load button










Click Here to download source code


Creating TextFile Dynamically and using in Android


Click Here to download source code

Package Name   :   selva.file

Project Name     :   File1

Version                   :   1.5 ( Supports 1.5 and above versions)


 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Please enter some text"/>
    <EditText
    android:id="@+id/txtText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
    <Button
    android:id="@+id/btnSave"
    android:text="Save"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
    <EditText
    android:id="@+id/txtText2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
    <Button
    android:id="@+id/btnLoad"
    android:text="Load"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>


 File1Activity.java

package selva.file;

import android.app.Activity;
import android.os.Bundle;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class File1Activity extends Activity
{
    private EditText textBox,textBox1;
    private static final int READ_BLOCK_SIZE = 100;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textBox = (EditText) findViewById(R.id.txtText1);
        textBox1 = (EditText) findViewById(R.id.txtText2);
        Button saveBtn = (Button) findViewById(R.id.btnSave);
        Button loadBtn = (Button) findViewById(R.id.btnLoad);
        saveBtn.setOnClickListener(new View.OnClickListener()
            {
            public void onClick(View v)
            {
                String str = textBox.getText().toString();
                try
                {
                    FileOutputStream fOut =openFileOutput("textfile.txt",MODE_WORLD_READABLE);
                    OutputStreamWriter osw = new OutputStreamWriter(fOut);
                    //---write the string to the file---
                    osw.write(str);
                    osw.flush();
                    osw.close();
                    //---display file saved message---
                    Toast.makeText(getBaseContext(),"File saved successfully!",Toast.LENGTH_SHORT).show();
                    //---clears the EditText---
                    textBox.setText("");
                }
                catch (IOException ioe)
                {
                    ioe.printStackTrace();
                }
            }
            });
        loadBtn.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                try
                {
                    FileInputStream fIn =openFileInput("textfile.txt");
                    InputStreamReader isr = new InputStreamReader(fIn);
                    char[] inputBuffer = new char[READ_BLOCK_SIZE];
                    String s = "";
                    int charRead;
                    while ((charRead = isr.read(inputBuffer))>0)
                    {
                        //---convert the chars to a String---
                        String readString =String.copyValueOf(inputBuffer, 0,charRead);
                        s += readString;
                        inputBuffer = new char[READ_BLOCK_SIZE];
                    }
                    //---set the EditText to the text that has been
                    // read---
                    textBox1.setText(s);
                    Toast.makeText(getBaseContext(),"File loaded successfully!",Toast.LENGTH_SHORT).show();
                }
                catch (IOException ioe)
                {
                    ioe.printStackTrace();
                }
            }
        });
    }
}



 OUTPUT:
































Enter some text and click save 

 















































Click Load Button



 







































textfile.txt present in DDMS --> File Explorer --> selva.file (Your Package Name)
 --> File --> textfile.txt


open DDMS




Click File Explorer







Expand selva.file




































Expand files












Click Here to download source code


Store Data in Mobile Memory using SharedPreference



Click Here to download source code

Package Name   :   selva.shared

Project Name     :   SharedPreferences

Version              :   1.5 ( Support 1.5 and above versions)


main.xml

<?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" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

 
    <Button
        android:id="@+id/button1"
        android:layout_width="98dp"
        android:layout_height="wrap_content"
        android:text="Save" />

</LinearLayout>



m1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <Button
        android:id="@+id/button"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:text="View Saved Data" />
   
</LinearLayout>


SharedPreferencesActivity.java


package selva.shared;

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

public class SharedPreferencesActivity extends Activity
{
     public SharedPreferences prefs;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button btn=(Button) findViewById(R.id.button1);
       
      btn.setOnClickListener(new View.OnClickListener()
      {
       
        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
       
               String str="www.androidprogramz.in";
                int no=234; 
                float fno=234.234f;
               
                prefs = getSharedPreferences("detail", MODE_PRIVATE);
                      // detail is sharedpreference name
                SharedPreferences.Editor editor = prefs.edit();
               
                editor.putString("Urlname",str);
                // To save str value in Urlname
                editor.putInt("Intvalue", no);
               // To save no value in Intvalue
                editor.putFloat("Floatvalue", fno);
                
// To save fno value in Floatvalue
                editor.commit();
           
           
            Intent i=new Intent(SharedPreferencesActivity.this,Anotheractivity.class);
            startActivity(i);
           
        }
    });
       
       
    }
}



Anotheractivity.java



package selva.shared;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Anotheractivity extends Activity
{
    public SharedPreferences prefs;
   
      public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.m1);
       
            Button btn=(Button) findViewById(R.id.button);
   
             prefs = getSharedPreferences("detail", MODE_PRIVATE);
            
                final String name=prefs.getString("Urlname", "novalue");
                     // To get name value from Urlname
               final int val=prefs.getInt("Intvalue", 0);
                       // To get val value from Intvlue
               final float fval=prefs.getFloat("Floatvalue", 0);
                     
// To get fval value from Floatvalue
            btn.setOnClickListener(new View.OnClickListener()
            {
               
                @Override
                public void onClick(View v)
                {
                    // TODO Auto-generated method stub
                LinearLayout l=(LinearLayout) findViewById(R.id.linearlayout);
               
                TextView tv=new TextView(Anotheractivity.this);
                tv.setText(name);
                tv.setTextColor(Color.GREEN);
                l.addView(tv);
               
                TextView tv1=new TextView(Anotheractivity.this);
                tv1.setText(Integer.toString(val));
                tv1.setTextColor(Color.GREEN);
                l.addView(tv1);
               
                TextView tv2=new TextView(Anotheractivity.this);
                tv2.setText(Float.toString(fval));
                tv2.setTextColor(Color.GREEN);
                l.addView(tv2);
               
           
                }
            });
             
             
        }

}

   note : to clear sharedpreference 

               prefs = getSharedPreferences("detail", MODE_PRIVATE);
                    
                SharedPreferences.Editor editor = prefs.edit();
              
                editor.clear(); // to clear all values in detail                

                editor.commit();

   note 1: to remove one particular value

              prefs = getSharedPreferences("detail", MODE_PRIVATE);
                    
                SharedPreferences.Editor editor = prefs.edit();
              
                editor.remove("Urlname"); // to remove paricular value from detail                

                editor.commit();



  AndroidManifest.xml


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

    <uses-sdk android:minSdkVersion="3" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SharedPreferencesActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
             android:label="@string/app_name"
             android:name=".Anotheractivity">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

OUTPUT:





















click save button and View Saved Data











































Click Here to download source code