Today we are developing Simple "Message Push App" that push messages from your app to default Messaging App of Android.
In one of my project I found I situation where I am taking some message from my Web service and I need to put this message into Original Messaging App of Android to provide naturality in Message and All other Task related to Messaging Such as Read message Status,Unread Status and all other options responsibility will be handled by Android Default Messaging App.
To do this we need
getContentResolver().insert(Uri.parse("content://sms/inbox"),
contentValues);
two permissions as
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
Just Create New project As PushMessage.java
Your main class PushMessage.java
public class PushMessage extends Activity {
EditText
editTextTitle;
EditText
editTextBody;
Button
cancelbtn;
Button
pushSMSbtn;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_push_message);
/**
* Setting UI
*/
editTextTitle = (EditText)
findViewById(R.id.editTextTitle);
editTextBody = (EditText)
findViewById(R.id.editTextTitle);
cancelbtn = (Button)
findViewById(R.id.btnCancel);
pushSMSbtn = (Button)
findViewById(R.id.btnPushSms);
/**
* Seating On click listener
*/
cancelbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
/**
* Seating On click listener
*/
pushSMSbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pushMesgIntoInbox();
}
});
}
/**
* This Method push Message Into Inbox
*/
public void pushMesgIntoInbox()
{
String
title = editTextTitle.getText().toString();
String
body = editTextBody.getText().toString();
/**
* This requires <uses-permission
*
android:name="android.permission.READ_SMS" /> <uses-permission
*
android:name="android.permission.WRITE_SMS" />
*/
if (!title.equals("")) {
try {
/** Creating Simple New Message
*/
ContentValues contentValues = new ContentValues();
contentValues.put("address",
title);
contentValues.put("body",
body);
contentValues.put("date", ava.lang.System.currentTimeMillis());
/** Message
*/
getContentResolver().insert(Uri.parse("content://sms/inbox"),
contentValues);
Toast.makeText(getApplicationContext(),
"Message
Pushed",
Toast.LENGTH_LONG).show();
// set EditText to
empty
editTextBody.setText("");
editTextTitle.setText("");
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"No Messaging
App Found. " + e,
Toast.LENGTH_LONG).show();
}
}
else {
Toast.makeText(getApplicationContext(),
"Please Enter
Title" , Toast.LENGTH_LONG)
.show();
}
}
}
And xml layout activity_push_message.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="@+id/editTextTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="SMS Title / Number " />
<EditText
android:id="@+id/edittextBody"
android:layout_width="match_parent"
android:layout_height="222dp"
android:ems="10"
android:hint="Message Body"
android:singleLine="false" />
<Button
android:id="@+id/btnPushSms"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Push SMS To Inbox" />
<Button
android:id="@+id/btnCancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0"
encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.blogspot.khurramitdeveloper"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="in.blogspot.khurramitdeveloper.PushMessage"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
Thats end You had done :)
Now run Your App
Hope it is useful to you :)
Happy Codding :)