如何在执行时更新Android应用程序

如何在执行时更新Android应用程序,android,database,sms,android-emulator,Android,Database,Sms,Android Emulator,嘿,我在安卓系统中尝试一些东西,必须用短信发送文字并在模拟器屏幕上打印。但该应用程序没有及时更新 我发一条信息。好啊但当我打开应用程序时,它不会打印我发送的文字。所以我必须关闭模拟器,然后再次打开它,然后运行应用程序,单词就在那里 我希望当我发送消息,然后我访问应用程序时,这个词是打印出来的,而不必关闭模拟器 我该怎么做 这是我的代码。请记住,我想发送一条新短信,当我再次进入我的应用程序时(不关闭并打开模拟器),它会更新屏幕上的单词,新单词将打印在屏幕上。Thx 我有一个类SMS.java,它是

嘿,我在安卓系统中尝试一些东西,必须用短信发送文字并在模拟器屏幕上打印。但该应用程序没有及时更新

我发一条信息。好啊但当我打开应用程序时,它不会打印我发送的文字。所以我必须关闭模拟器,然后再次打开它,然后运行应用程序,单词就在那里

我希望当我发送消息,然后我访问应用程序时,这个词是打印出来的,而不必关闭模拟器

我该怎么做

这是我的代码。请记住,我想发送一条新短信,当我再次进入我的应用程序时(不关闭并打开模拟器),它会更新屏幕上的单词,新单词将打印在屏幕上。Thx

我有一个类SMS.java,它是我的活动,还有另一个类,在这个类下面,叫做DataHelper.java,它操作我的数据库,并有插入、查询、更新等方法:

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());
                }                   
        }             
   }
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);
  }
 }
}
package com.sys;
导入java.util.List;
导入java.util.StringTokenizer;
导入com.sys.DataHelper;
导入android.app.Activity;
导入android.content.ContentValues;
导入android.content.Intent;
导入android.database.Cursor;
导入android.database.SQLException;
导入android.database.sqlite.SQLiteDatabase;
导入android.net.Uri;
导入android.os.Bundle;
导入android.provider.BaseColumns;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.sys.ratedsmcontacts;
公共类SMS扩展活动{
私人数据助理卫生署;
私有静态文本视图txtView;
专用静态按钮btnChkCntts;
专用静态按钮btnChkTag;
最终Uri内容\u Uri=Uri.parse(“content://sms/sent");
@凌驾
创建时的公共void(Bundle savedInstanceState){
this.dh=新的数据助手(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
最终按钮按钮=(按钮)findViewById(R.id.btnChkCntts);
setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
Intent myIntent=newintent(v.getContext(),ratedmscontacts.class);
startActivityForResult(myIntent,0);
}
});
txtView=(TextView)findViewById(R.id.txtView);
Cursor Cursor=getContentResolver().query(CONTENT\u URI,null,null,null);
弦体;
弦乐;
if(cursor.moveToFirst()){
body=cursor.getString(cursor.getColumnIndexOrThrow(“body”)).toString();
如果(正文==“”){
Toast.makeText(getBaseContext(),“没有要保存的单词!”,Toast.LENGTH\u LONG.show();
}
否则{
StringTokenizer st=新的StringTokenizer(主体);
而(st.hasMoreTokens()){
atual=st.nextToken();
while(this.dh.select(atual)==true){
这是dh.atualiza(atual);
Toast.makeText(getBaseContext(),“Word已更新!”,Toast.LENGTH_LONG.show();
}
while(this.dh.select(atual)==false){
本.dh.插入(atual);
Toast.makeText(getBaseContext(),“Word已添加!”,Toast.LENGTH_LONG.show();
}
}
}
List words=this.dh.selectAll();
StringBuilder sb=新的StringBuilder();
sb.append(“一组单词:\n\n”);
for(字符串w:单词){
某人附加(w);
某人加上(“”);
}   
setxt(sb.toString());
}                   
}             
}
DataHelper.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());
                }                   
        }             
   }
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);
  }
 }
}
package com.sys;
导入android.content.Context;
导入android.database.Cursor;
导入android.database.sqlite.SQLiteDatabase;
导入android.database.sqlite.SQLiteOpenHelper;
导入android.database.sqlite.SQLiteStatement;
导入android.util.Log;
导入android.widget.Toast;
导入java.util.ArrayList;
导入java.util.List;
公共类DataHelper{
私有静态最终字符串数据库\u NAME=“sms.db”;
私有静态最终int数据库_VERSION=1;
私有静态最终字符串表\u NAME=“words”;
私人语境;
专用数据库数据库;
私有SQLiteStatement insertStmt;
私有SQLiteStatement updateStmt;
私有静态最终字符串INSERT=“插入到”
+表_NAME+“(字,续)值(?,1)”;
公共DataHelper(上下文){
this.context=上下文;
OpenHelper OpenHelper=新的OpenHelper(this.context);
this.db=openHelper.getWritableDatabase();
//onUpgrade(db,1,2);
this.insertStmt=this.db.compileStatement(INSERT);
}
公共长插入(字符串字){
this.insertStmt.bindString(1,word);
返回此.insertStmt.executeInsert();
}
公共无效atualiza(字符串字){
this.db.execSQL(“更新单词集cont=cont+1,其中(word=?)”,新字符串[]{word});
}
public void deleteAll(){
this.db.delete(表名称,null,null);
}
公共布尔选择(字符串wrd){
字符串字;
Cursor Cursor=this.db.query(表名称,新字符串[]{“word”},“word like?”,新字符串[]{wrd},null,null,null);
if(cursor.moveToFirst()){
做{
word=cursor.getString(0);
}while(cursor.moveToNext());
}
if(cursor!=null&!cursor.isClosed()){
cursor.close();
返回true;
}否则{
返回false;
}
}
公共列表selectMaxCont(){
列表=新的ArrayList();
Cur