Tuesday 5 November 2013

Call And SMS Project

If you want to create a app from Calling or SMS then this is a best example for you. Here i am going to create a app where i can call to my contact list or can SMS to a specified number 

Example:






Code:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/contactlist"
            android:src="@drawable/contact" />

        <Button
            android:id="@+id/button1"
            android:layout_width="220dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:text="@string/callfromcontact" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="15dp"
            android:contentDescription="@string/sms"
            android:src="@drawable/sms" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="15dp"
            android:ems="10"
            android:hint="@string/enterphoneno"
            android:inputType="number" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="73dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:ems="10"
            android:hint="@string/msg"
            android:inputType="textPostalAddress" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="@string/smssend" />

    </LinearLayout>

</LinearLayout>


MainActivity.java

package in.androiddevelopmentanddiscussion.callandsmsexample;

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;

public class MainActivity extends Activity {

EditText phno,msg;
Button sms,call;
String phoneno,message;

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

phno = (EditText)findViewById(R.id.editText1);
msg = (EditText)findViewById(R.id.editText2);
sms = (Button)findViewById(R.id.button2);
call = (Button)findViewById(R.id.button1);

sms.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

phoneno = phno.getText().toString();
message = msg.getText().toString();

try
{
SmsManager smanager = SmsManager.getDefault();
smanager.sendTextMessage(phoneno, null, message, null, null);
Toast.makeText(MainActivity.this, "Message Sent", Toast.LENGTH_SHORT).show();
phno.setText(" ");
msg.setText(" ");
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "Message sending failed", Toast.LENGTH_SHORT).show();
e.printStackTrace();

}

}
});


call.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
try
{
           Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
           intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
           startActivityForResult(intent, 1); 
        
}
catch(Exception exx)
{
exx.printStackTrace();
}
}
});


}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (data != null) {
       Uri uri = data.getData();

       if (uri != null) {
           Cursor c = null;
           try {
               c = getContentResolver().query(uri, new String[]{ 
                           ContactsContract.CommonDataKinds.Phone.NUMBER,  
                           ContactsContract.CommonDataKinds.Phone.TYPE },
                       null, null, null);

               if (c != null && c.moveToFirst()) {
                   String number = c.getString(0);
                   int type = c.getInt(1);
                   showSelectedNumber(type, number);
               }
           } finally {
               if (c != null) {
                   c.close();
               }
           }
       }
   }
}


public void showSelectedNumber(int type, String number) {
   //Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show();      

try
{
String num = "tel:+91"+number;
Intent i = new Intent(Intent.ACTION_CALL);
i.setData(Uri.parse(num));
startActivity(i);
}
catch(Exception exxx)
{
exxx.printStackTrace();
}


}

}


Manifest

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

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


Download full project from here



Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

No comments:

Post a Comment