In this tutorial, you'll send a Message from thi application and also read Message which is forward by other person to you. In this you have to just write phone number and the message which you want to send. think this code is more helpfull to you.
Now, first of all create your android project and use this code for send Message.
SMSSend :
public class SMSSend extends Activity
{
Button send,read;
EditText no,msg;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
send=(Button) findViewById(R.id.btn_send);
read=(Button) findViewById(R.id.btn_read);
no=(EditText) findViewById(R.id.et_no);
msg=(EditText) findViewById(R.id.et_msg);
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String no1=no.getText().toString();
String msg1=msg.getText().toString();
sendSms(no1,msg1);
}
});
read.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),SMSRead.class);
startActivity(i);
}
});
}
public void sendSms(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
// after SMS sending
registerReceiver(new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent) {
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));
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, sentPI,null);
}
}
For the read message you can use this code.
SMSRead :
public class SMSRead extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView read =new TextView(this);
Uri uri = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uri,
new String[] { "_id", "thread_id", "address", "person", "date", "body" },
null,null,null);
String address="",body="",time="";
long timestamp=0;
if (cursor != null)
{
try
{
int count = cursor.getCount();
if (count > 0)
{
cursor.moveToFirst();
long messageId = cursor.getLong(0);
long threadId = cursor.getLong(1);
address = cursor.getString(2);
long contactId = cursor.getLong(3);
String contactId_string = String.valueOf(contactId);
body = cursor.getString(5);
}
}
finally { cursor.close(); }
read.setText("Phone : "+ address "\n SMS : \n" + body);
setContentView(read);
}
}
}
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="@string/hello"
/>
<EditText android:hint="Enter No" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/et_no"/>
<EditText android:hint="Enter Message" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/et_msg"/>
<Button android:text="Send" android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button android:text="Read" android:id="@+id/btn_read" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Android uses a permission-based policy where all the permissions needed by an application need to be specified in the
In the
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.empmark.SMSSend"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="com.empmark.SMSSend.SMSSend"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.empmark.SMSRead.SMSRead" />
</application>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
</manifest>
Screen Shorts :
Now, first of all create your android project and use this code for send Message.
SMSSend :
public class SMSSend extends Activity
{
Button send,read;
EditText no,msg;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
send=(Button) findViewById(R.id.btn_send);
read=(Button) findViewById(R.id.btn_read);
no=(EditText) findViewById(R.id.et_no);
msg=(EditText) findViewById(R.id.et_msg);
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String no1=no.getText().toString();
String msg1=msg.getText().toString();
sendSms(no1,msg1);
}
});
read.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),SMSRead.class);
startActivity(i);
}
});
}
public void sendSms(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
// after SMS sending
registerReceiver(new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent) {
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));
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, sentPI,null);
}
}
For the read message you can use this code.
SMSRead :
public class SMSRead extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView read =new TextView(this);
Uri uri = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uri,
new String[] { "_id", "thread_id", "address", "person", "date", "body" },
null,null,null);
String address="",body="",time="";
long timestamp=0;
if (cursor != null)
{
try
{
int count = cursor.getCount();
if (count > 0)
{
cursor.moveToFirst();
long messageId = cursor.getLong(0);
long threadId = cursor.getLong(1);
address = cursor.getString(2);
long contactId = cursor.getLong(3);
String contactId_string = String.valueOf(contactId);
body = cursor.getString(5);
}
}
finally { cursor.close(); }
read.setText("Phone : "+ address "\n SMS : \n" + body);
setContentView(read);
}
}
}
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="@string/hello"
/>
<EditText android:hint="Enter No" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/et_no"/>
<EditText android:hint="Enter Message" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/et_msg"/>
<Button android:text="Send" android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button android:text="Read" android:id="@+id/btn_read" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Android uses a permission-based policy where all the permissions needed by an application need to be specified in the
AndroidManifest.xml
file. By doing so, when the application is installed it will be clear to the user what specific access permissions are required by the application. For example, as sending SMS messages will potentially incur additional cost on the user's end, indicating the SMS permissions in the AndroidManifest.xml
file will let the user decide whether to allow the application to install or not.In the
AndroidManifest.xml
file, add the two permissions - SEND_SMS , READ SMS and RECEIVE_SMS:<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.empmark.SMSSend"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="com.empmark.SMSSend.SMSSend"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.empmark.SMSRead.SMSRead" />
</application>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
</manifest>
Screen Shorts :
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.