嘿,我嘗試了一些在android中的東西,必須獲取以短信形式發送的文字並在模擬器屏幕上打印。但該應用程序沒有及時更新。如何在執行時間更新Android應用程序
我發送消息。好。但是,當我打開應用程序它不打印我發送的單詞。所以我必須關閉模擬器然後再打開它,然後運行該應用程序,並且該單詞在那裏。
像這樣的Id當我發送消息,然後我訪問應用程序,單詞打印沒有我必須關閉模擬器。
我該怎麼做?
所以這裏是我的代碼。請記住,我想發送一條新短信,當我再次訪問我的應用程序時(不關閉並打開模擬器),它會更新屏幕中的文字,新的文字將顯示在屏幕上。謝謝。
我有這個類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());
}
}
}
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);
}
}
}
男人它工作得很好!非常感謝! – rogcg 2010-10-26 05:04:24