如何使执行时间上的Andr​​oid应用程序更新 [英] How to make Android app update in execution time

查看:91
本文介绍了如何使执行时间上的Andr​​oid应用程序更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我试着一些在Android中,有获得在短信发送的文字和打印仿真器屏幕上。但在时间上的应用程序犯规更新。

我发送消息。好。但是,当我打开应用程序,它不打印,我已经发了话。所以,我必须要关闭模拟器,然后再次打开它,并运行应用程序,这个词是存在的。

标识一样,当我发出的邮件,然后我访问的应用程序,这个词是不打印我不得不关闭模拟器。

我如何做到这一点?

因此​​,这里是我的code。请记住,我想,发送新的短信,当我去我的应用程序再次,(不关闭和打开仿真器),它更新在屏幕上的字,新词应当印在屏幕上。 THX。

我有这个类的 SMS.java ,这是我的活动,和另一个类,即低于这个人在这里,所谓的 DataHelper.java ,它操纵我的数据库,并具有方法来插入,查询,更新等:

SMS.java

 包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.ratedSmsContacts;
公共类短信延伸活动{

私人DataHelper DH;
私有静态的TextView txtView;
私有静态按钮btnChkCntts;
私有静态按钮btnChkTag;

最后乌里CONTENT_URI = Uri.parse(内容://短信/派);
    @覆盖
    公共无效的onCreate(包savedInstanceState){
    this.dh =新DataHelper(本);
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    最后Button按钮=(按钮)findViewById(R.id.btnChkCntts);
    button.setOnClickListener(新View.OnClickListener(){
    公共无效的onClick(视图v){
        意图myIntent =新的意图(v.getContext(),ratedSmsContacts.class);
        startActivityForResult(myIntent,0);
    }
    });

    txtView =(TextView中)findViewById(R.id.txtView);
        。光标光标= getContentResolver()查询(CONTENT_URI,NULL,NULL,NULL,NULL);
        字符串的身体;
        字符串atual;
        如果(cursor.moveToFirst()){
            身体= cursor.getString(cursor.getColumnIndexOrThrow(身体))的toString()。
            如果(身体==){
                Toast.makeText(getBaseContext()!有没有话要救,Toast.LENGTH_LONG).show();
            }
                其他{
                    StringTokenizer的ST =新的StringTokenizer(身体);
                    而(st.hasMoreTokens()){
                        atual = st.nextToken();
                            而(this.dh.select(atual)==真){
                                this.dh.atualiza(atual);
                                Toast.makeText(getBaseContext(),字更新!,Toast.LENGTH_LONG).show();
                            }
                            而(this.dh.select(atual)== FALSE){
                                this.dh.insert(atual);
                                Toast.makeText(getBaseContext(),字说!,Toast.LENGTH_LONG).show();
                            }
                        }
                    }
                    名单<字符串>也就是说= this.dh.selectAll();
                    StringBuilder的SB =新的StringBuilder();
                    sb.append(组词:\ñ\ N);
                    对于(字符串W:字){
                        sb.append(瓦特);
                        sb.append();

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

DataHelper.java

 包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 {

私有静态最后弦乐DATABASE_NAME =sms.db;
私有静态最终诠释DATABASE_VERSION = 1;
私有静态最后弦乐TABLE_NAME =字;

私人上下文的背景下;
私人SQLiteDatabase分贝;

私人SQLiteStatement的insertstmt;
私人SQLiteStatement updateStmt;
私有静态最后弦乐INSERT =插入
  + TABLE_NAME +(字,续)值(1?);

公共DataHelper(上下文的背景下){
  this.context =背景;
  OpenHelper openHelper =新OpenHelper(this.context);
  this.db = openHelper.getWritableDatabase();
  //openHelper.onUpgrade(db,1,2);
  this.insertStmt = this.db.compileStatement(INSERT);
 }

众长刀片(串词){
  this.insertStmt.bindString(1字);
  返回this.insertStmt.executeInsert();
 }

公共无效atualiza(串词){
   this.db.execSQL(更新字数设置续=续+ 1 WHERE(字=)?,新的String [] {文字});
 }

公共无效用deleteAll(){
  this.db.delete(TABLE_NAME,NULL,NULL);
 }

公共布尔选择(字符串WRD){
  串词;
  光标光标= this.db.query(TABLE_NAME,新的String [] {字},字怎么样?,新的String [] {} WRD,NULL,NULL,NULL);
  如果(cursor.moveToFirst()){
     做 {
        字= cursor.getString(0);
     }而(cursor.moveToNext());
   }
  如果(光标=空&安培;!&安培;!cursor.isClosed()){
     cursor.close();
     返回true;
  }其他{
      返回false;
  }
}

公开名单<字符串> selectMaxCont(){
   名单<字符串>名单=新的ArrayList<字符串>();
   光标光标= this.db.query(TABLE_NAME,新的String [] {字},NULL,NULL,NULL,NULL,续递减);
   如果(cursor.moveToFirst()){
       做{
   list.add(cursor.getString(0));
       }而(cursor.moveToNext());
   }
   如果(光标=空&安培;!&安培;!cursor.isClosed()){
       cursor.close();
   }
   返回列表;
}

公开名单<字符串>全选() {
      名单<字符串>名单=新的ArrayList<字符串>();
      光标光标= this.db.query(TABLE_NAME,新的String [] {字},NULL,NULL,NULL,NULL,续递减);
      如果(cursor.moveToFirst()){
         做 {
            list.add(cursor.getString(0).toUpperCase());
         }而(cursor.moveToNext());
      }
      如果(光标=空&安培;!&安培;!cursor.isClosed()){
         cursor.close();
      }
      返回列表;
   }

私有静态类OpenHelper扩展SQLiteOpenHelper {

  OpenHelper(上下文的背景下){
     超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
  }

  @覆盖
  公共无效的onCreate(SQLiteDatabase DB){
     db.execSQL(CREATE TABLE+ TABLE_NAME +(ID INTEGER PRIMARY KEY,字文,续INTEGER));
  }

  @覆盖
  公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
     Log.w(短信词数据库,升级数据库,这将删除表并重新创建。);
     db.execSQL(DROP TABLE IF EXISTS+ TABLE_NAME);
     的onCreate(DB);
  }
 }
}
 

解决方案

您可以将所有的大部分code在OnCreate()到您的活动中onResume。基本上你应该查询onResume中的内容解析,让你得到一组新的数据,当你的活动是在前台。如果没有这一点,你得到一个快照创建的活动时。这是怎么回事,当你去到短信界面被暂停后又重新开始时,你回去吧。这是还活着,即使你切换应用程序这样的onCreate将不会被调用,直到活动被杀死或完成,然后遭遇到。

因此​​,在短期举动ContentResolver.query()到onResume。并显示您的状态存在。

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);
  }
 }
}

解决方案

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.

这篇关于如何使执行时间上的Andr​​oid应用程序更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆