Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3280148
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:40:18+00:00 2026-05-17T19:40:18+00:00

Hey, Im trying something in android, that has to get the words sent in

  • 0

Hey, Im trying something in android, that has to get the words sent in sms and print on the emulator screen. But the app doesnt update in time.

I send a message. Ok. But when I open the app it doesnt print the words that I’ve sent. So i have to close the emulator and then open it again, and run the app, and the word is there.

Id like that when I sent the message and then I access the app, the word is printed without I have to close the emulator.

How do I do this?

So here is my code. Remember that I want to, send a new SMS and when I go to my app again, (without close and open the emulator), it updates the words in the screen, the new words shall be printed in the screen. Thx.

I have this class SMS.java, which is my activity, and another class, that is below this one here, called DataHelper.java, which manipulate my database, and has the methods to insert, query, update, etc.:

SMS.java

package com.sys;

import java.util.List;
import java.util.StringTokenizer;

import com.sys.DataHelper;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.sys.ratedSmsContacts;
public class SMS extends Activity { 

private DataHelper dh;  
private static TextView txtView;                
private static Button btnChkCntts;
private static Button btnChkTag;

final Uri CONTENT_URI = Uri.parse("content://sms/sent");
    @Override
    public void onCreate(Bundle savedInstanceState) {
    this.dh = new DataHelper(this); 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button button = (Button) findViewById(R.id.btnChkCntts);
    button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Intent myIntent = new Intent(v.getContext(), ratedSmsContacts.class);
        startActivityForResult(myIntent, 0);
    }
    });

    txtView =    (TextView)findViewById(R.id.txtView);      
        Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null, null);                                    
        String body;  
        String atual;
        if(cursor.moveToFirst()){
            body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
            if(body == " "){
                Toast.makeText(getBaseContext(), "There is no words to save!", Toast.LENGTH_LONG).show();
            }
                else{                               
                    StringTokenizer st = new StringTokenizer(body);                   
                    while(st.hasMoreTokens()){
                        atual = st.nextToken();
                            while(this.dh.select(atual) == true){
                                this.dh.atualiza(atual);
                                Toast.makeText(getBaseContext(), "Word updated!", Toast.LENGTH_LONG).show();
                            }
                            while(this.dh.select(atual) == false){
                                this.dh.insert(atual);
                                Toast.makeText(getBaseContext(), "Word added!", Toast.LENGTH_LONG).show();
                            }
                        }
                    }
                    List<String> words = this.dh.selectAll();
                    StringBuilder sb = new StringBuilder();
                    sb.append("Set of words:\n\n");
                    for (String w : words) {
                        sb.append(w);
                        sb.append(" ");                         

                    }   
                    txtView.setText(sb.toString());
                }                   
        }             
   }

DataHelper.java

package com.sys;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class DataHelper {

private static final String DATABASE_NAME = "sms.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "words";

private Context context;
private SQLiteDatabase db;

private SQLiteStatement insertStmt;
private SQLiteStatement updateStmt;
private static final String INSERT = "insert into " 
  + TABLE_NAME + "(word, cont) values (?, 1)";

public DataHelper(Context context) {
  this.context = context;
  OpenHelper openHelper = new OpenHelper(this.context);
  this.db = openHelper.getWritableDatabase();         
  //openHelper.onUpgrade(db, 1, 2);
  this.insertStmt = this.db.compileStatement(INSERT);
 }

public long insert(String word) { 
  this.insertStmt.bindString(1, word);             
  return this.insertStmt.executeInsert();
 }

public void atualiza(String word){
   this.db.execSQL("UPDATE words SET cont = cont + 1 WHERE (word= ?)", new String[] {word} );      
 }

public void deleteAll() {
  this.db.delete(TABLE_NAME, null, null);
 }

public boolean select(String wrd) {
  String word;
  Cursor cursor = this.db.query(TABLE_NAME, new String[] {"word"}, "word like ?", new String[] {wrd} , null, null, null);
  if (cursor.moveToFirst()) {
     do {
        word = cursor.getString(0); 
     } while (cursor.moveToNext());
   }
  if (cursor != null && !cursor.isClosed()) {
     cursor.close();         
     return true;         
  }else{
      return false;
  }
}

public List<String> selectMaxCont(){       
   List<String> list = new ArrayList<String>();
   Cursor cursor = this.db.query(TABLE_NAME, new String[]{"word"}, null, null, null, null, "cont desc");
   if(cursor.moveToFirst()){
       do{
   list.add(cursor.getString(0));
       }while(cursor.moveToNext());
   }
   if(cursor != null && !cursor.isClosed()){
       cursor.close();
   }
   return list;
}

public List<String> selectAll() {
      List<String> list = new ArrayList<String>();
      Cursor cursor = this.db.query(TABLE_NAME, new String[] { "word" }, null, null, null, null, "cont desc");
      if (cursor.moveToFirst()) {
         do {
            list.add(cursor.getString(0).toUpperCase()); 
         } while (cursor.moveToNext());
      }
      if (cursor != null && !cursor.isClosed()) {
         cursor.close();
      }
      return list;
   }

private static class OpenHelper extends SQLiteOpenHelper {

  OpenHelper(Context context) {
     super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
     db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, word TEXT, cont INTEGER)");         
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
     Log.w("SMS Words Database", "Upgrading database, this will drop tables and recreate.");
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
     onCreate(db);
  }
 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:40:19+00:00Added an answer on May 17, 2026 at 7:40 pm

    You could move all most of the code in the OnCreate() to onResume within your activity. Basically you should query the content resolver within the onResume so that you get a fresh set of the data when your activity is in the foreground. Without this you are getting a snapshot when the activity is created. It’s going to be paused when you go to the SMS screen and then resumed when you go back to it. It is still alive even though you switched apps so the onCreate won’t be called until the activity is either killed or finished and then rentered.

    So in short move the ContentResolver.query() to the onResume. And display your state there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey everyone, I am trying to run the following program, but am getting a
Hey, I am trying to write a command to print todays date on the
Hey! I am not trying to push my luck here but I have another
Hey having some trouble trying to maintain transparency on a png when i create
Hey everyone. I'm trying to make a swing GUI with a button and a
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey, I'm using Levenshteins algorithm to get distance between source and target string. also
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey so what I want to do is snag the content for the first
Hey all, my Computational Science course this semester is entirely in Java. I was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.