sms retriving

News, information, contests and special event announcements from members of the anddev.org community.

sms retriving

Postby suman » Fri Dec 26, 2008 9:19 am

i have your sms notification program. i have submit the code


import android.content.BroadcastReceiver;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SMSReceiver 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());
sb.append("\n----Message----\n");
// Actual Message-Content
sb.append(currentMessage.getDisplayMessageBody());
//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);
////////////////////////////////////////////
//insert in my db
///ID Phone No Message Date Read/Unread


///////////////////////////////////////////
}
}
// 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, SMSActivity.class);
//i.putExtra("nikvar", "sb");
// i.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
//i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

}
}
}
Main activity class



import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class SMSActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Log.v(SMSReceiver.LOG_TAG,"inside SMS Activity");
}
}



<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.anddev.android.smsexample">
<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=".SMSActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- This class will react on the SMS show a notification
and start the Main-App afterwards -->
<receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>


i have my 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));
}




}



you please give me the code by which i can insert the incomming sms into the database.
plese help me. I have tried so much. but i cant send anything from the receiver class. And from the main activity i cant access the sms. Intent is not working there. please help me soon.
IPEG SOLUTIONS
suman
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Dec 26, 2008 8:15 am
Location: 696B BLOCK A KOL WESTBENGAL(INDIA)

Top

Postby Quetzalcoatl » Tue Jan 13, 2009 9:00 pm

Is this the best place for a coding query?
Image
Quetzalcoatl
Senior Developer
Senior Developer
 
Posts: 129
Joined: Sat Oct 18, 2008 8:21 pm
Location: near Manchester, UK

Postby MrSnowflake » Tue Jan 13, 2009 9:06 pm

This is the WORST place to post some coding questions... Unfortunately I'm not a News moderator...
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Re: sms retriving

Postby CusterS87 » Sun May 13, 2012 10:49 pm

Please, try to use tinypast.com to keep the format of the code..

this code is missed up!
CusterS87
Once Poster
Once Poster
 
Posts: 1
Joined: Sun May 13, 2012 10:46 pm

Top

Return to Open News

Who is online

Users browsing this forum: No registered users and 3 guests