my name is Suman. i can notify a incoming sms. the code is written
belowimport android.content.BroadcastReceiver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class SMSReceiver1 extends BroadcastReceiver {
/** TAG used for Debug-Logging */
protected static final String LOG_TAG = "SMSReceiver";
/** The Action fired by the Android-System when a SMS was
received.
* We are using the Default Package-Visibility */
private static final String ACTION =
"android.provider.Telephony.SMS_RECEIVED";
// @Override
public void onReceive(Context context, Intent intent) {
Log.i(LOG_TAG, "[inside onReceive] ");
if (intent.getAction().equals(ACTION)) {
StringBuilder sb = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage
[pdusObj.length];
for (int i = 0; i<pdusObj.length; i++) {
messages[i] = SmsMessage.createFromPdu ((byte
[]) pdusObj[i]);
}
Log.i(LOG_TAG, "[SMSApp Bundle] " + bundle.toString
());
// Feed the StringBuilder with all Messages found.
for (SmsMessage currentMessage : messages){
sb.append("Received compressed SMS\nFrom:
");
// Sender-Number
sb.append
(currentMessage.getDisplayOriginatingAddress());
String value2 =
currentMessage.getDisplayOriginatingAddress();
// String value4 = value2.toString();
sb.append("\n----Message----\n");
// Actual Message-Content
sb.append(currentMessage.getDisplayMessageBody
());
String value3 =
currentMessage.getDisplayMessageBody();
ContentValues contentValues = new
ContentValues();
contentValues.put
(android.provider.Telephony.Sms.BODY,
currentMessage.getDisplayMessageBody());
android.net.Uri uri
=context.getContentResolver().insert
(android.provider.Telephony.Sms.CONTENT_URI, contentValues);
// Intent myIntent = new Intent();
//myIntent.setClassName("com.android.sms14",
"com.android.sms14.SMSActivity1");
//myIntent.putExtra("com.android.HelloBye.heby",
"Hello Joe!"); // key/value pair, where key needs current package
prefix.
//myIntent.hasExtra("('niva', '"+value2+"')");
//myIntent.hasExtra("('niva', '"+value3+"')");
//myIntent.putExtra("niva", "Sengupta");//
key/value
pair, where key needs current package prefix.
//startActivity(myIntent);
//ContentValues contentValues = new
ContentValues();
// contentValues.put
(android.provider.Telephony.Sms.BODY,
currentMessage.getDisplayMessageBody());
//Uri uri =context.getContentResolver().insert
(android.provider.Telephony.Sms.CONTENT_URI, contentValues);
//sb.append(uri);
}
}
// Logger Debug-Output
Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + sb);
// Show the Notification containing the Message.
Toast.makeText(context, sb.toString(),
Toast.LENGTH_LONG).show();
// Consume this intent, that no other application will
notice it.
this.abortBroadcast();
// Start the Main-Activity
Intent i = new Intent(context, SMSActivity1.class);
i.setClassName("com.android.sms14",
"com.android.sms14.SMSActivity1");
i.putExtra("fname", "suman999");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
main activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class SMSActivity1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.sms1);
Log.v(SMSReceiver1.LOG_TAG,"inside SMS Activity");
}
}
mainfest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.sms14">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application android:icon="@drawable/icon">
<!-- The Main Activity that gets started by the IntentReceiver
listed below -->
<activity android:name=".SMSActivity1" 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=".valueandroid1" android:label="@string/
app_name">
</activity>
<!-- This class will react on the SMS show a notification
and start the Main-App afterwards -->
<receiver android:name=".SMSReceiver1">
<intent-filter>
<action
android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
Now, i have database also.
import java.util.ArrayList;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
public class queryandroid extends ListActivity {
private final String MY_DATABASE_NAME = "myCoolDB_2";
private final String MY_DATABASE_TABLE = "Users";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
EditText et = new EditText(this);
et.setSelection(et.getText().length());
/* Will hold the 'Output' we want to display at the end. */
ArrayList<String> results = new ArrayList<String>();
SQLiteDatabase myDB = null;
try {
/* Create the Database (no Errors if it already exists)
*/
myDB = this.openOrCreateDatabase(MY_DATABASE_NAME,
MODE_PRIVATE, null);
//myDB.execSQL("DROP TABLE " + MY_DATABASE_TABLE ) ;
/* Create a Table in the Database. */
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ MY_DATABASE_TABLE
+ " (MyKey INTEGER PRIMARY KEY
AUTOINCREMENT, MyName VARCHAR,"
+ " MyAge INT(3), MyDate DATE);");
//create table t1 (t1key INTEGER PRIMARY KEY,data
TEXT,num double,timeEnter DATE);
// UPDATE exam SET timeEnter = DATETIME('NOW') WHERE
rowid = new.rowid;
//myDB.execSQL("DELETE FROM " +
MY_DATABASE_TABLE ) ;
/* Add two DataSets to the Table. */
myDB.execSQL("INSERT INTO "
+ MY_DATABASE_TABLE
+ " (MyName, MyAge)"
+ " VALUES ('Nikhil Narayan', '27');");
myDB.execSQL("INSERT INTO "
+ MY_DATABASE_TABLE
+ " (MyName, MyAge)"
+ " VALUES ('Suman Ganguly', '22');");
/////////////////////////////////////////////////////////
//// Insert part
// String value2 = extras.getString("nikvar");
// String value3 = extras.getString("niva");
// String dfname = "Debosree ";
//String dlname = "Das";
//int dage = 24;
//String dcountry = "India";
//myDB.execSQL("INSERT INTO "
// + MY_DATABASE_TABLE
// + " (LastName, FirstName, Country, Age)"
// + " VALUES ('"+dfname+"', '"+dlname+"',
'"+dcountry+"', "+dage+");");
//// End Insert Part
/////////////////////////////////////////////////////////
/* Query for some results with Selection and
Projection. */
Cursor c = myDB.rawQuery("SELECT MyKey, MyName,MyAge"
+
" FROM " + MY_DATABASE_TABLE
+ " WHERE MyAge > 0 LIMIT
400;",
null);
/* Get the indices of the Columns we will need */
int MyNameColumn = c.getColumnIndexOrThrow("MyName") ;
int MyAgeColumn = c.getColumnIndexOrThrow("MyAge");
/* Check if our result was valid. */
c.moveToFirst();
if (c != null) {
/* Check if at least one Result was returned. */
if (c.isFirst()) {
int i = 0;
/* Loop through all Results */
do {
i++;
/* Retrieve the values of the Entry
* the Cursor is pointing to. */
String fullName = c.getString
(MyNameColumn);
int age = c.getInt(MyAgeColumn);
/* We can also receive the Name
* of a Column by its Index.
* Makes no sense, as we already
* know the Name, but just to show we
can */
String ageColumName = c.getColumnName
(MyAgeColumn);
String MyKeyVal = c.getString(0);
/* Add current Entry to results. */
results.add("" + i + ": " + fullName + "
Key= " +MyKeyVal
+ " (" + ageColumName +
": " + age + ")");
} while(c.moveToNext());
}
}
} finally {
if (myDB != null)
myDB.close();
}
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
}
}
Now my question is how can i insert the incoming messages into my
database? I have tried a lot. Please kindly give me the appropriate
code to solve the problem.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---