草庐IT

android - 关于检查应用程序启动 android 更新的新手问题

coder 2023-12-21 原文

我正在尝试下面的代码。它应该在应用程序启动时每天检查一次我的应用程序的更新版本。从 http://www.androidsnippets.com/check-for-updates-once-a-day 获得代码. 我无法弄清楚的是,在 URL updateURL = new URL("http://my.company.com/update"); 行中http://mycompany.com/update 应该是什么指向?一个 xml、txt 或 .. 文件。我尝试了一个只有最新版本代码的 xml 和 txt 文件,并将其命名为 version.txt、versionCode.txt 和 .xml。

它根本不起作用,URL 应该只是“http://mycompany.com/update”还是带有扩展名,即“http://mycompany.com/update/version.txt”或其他东西。

提前致谢。

public class Test extends Activity {
private Handler mHandler;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.front);
    mHandler = new Handler();

    /* Get Last Update Time from Preferences */
    SharedPreferences prefs = getPreferences(0);
    lastUpdateTime =  prefs.getLong("lastUpdateTime", 0);

    /* Should Activity Check for Updates Now? */
    if ((lastUpdateTime + (24 * 60 * 60 * 1000)) < System.currentTimeMillis()) {

        /* Save current timestamp for next Check*/
        lastUpdateTime = System.currentTimeMillis();            
        SharedPreferences.Editor editor = getPreferences(0).edit();
        editor.putLong("lastUpdateTime", lastUpdateTime);
        editor.commit();        

        /* Start Update */            
        checkUpdate.start();
    }
}

/* This Thread checks for Updates in the Background */
private Thread checkUpdate = new Thread() {
    public void run() {
        try {
            URL updateURL = new URL("http://my.company.com/update");                
            URLConnection conn = updateURL.openConnection(); 
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current = 0;
            while((current = bis.read()) != -1){
                 baf.append((byte)current);
            }

            /* Convert the Bytes read to a String. */
            final String s = new String(baf.toByteArray());         

            /* Get current Version Number */
            int curVersion = getPackageManager().getPackageInfo("your.app.id", 0).versionCode;
            int newVersion = Integer.valueOf(s);

            /* Is a higher version than the current already out? */
            if (newVersion > curVersion) {
                /* Post a Handler for the UI to pick up and open the Dialog */
                mHandler.post(showUpdate);
            }                
        } catch (Exception e) {
        }
    }
};

/* This Runnable creates a Dialog and asks the user to open the Market */ 
private Runnable showUpdate = new Runnable(){
       public void run(){
        new AlertDialog.Builder(Test.this)
        .setIcon(R.drawable.icon)
        .setTitle("Update Available")
        .setMessage("An update for is available!\\n\\nOpen Android Market and see the details?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                        /* User clicked OK so do some stuff */
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:your.app.id"));
                        startActivity(intent);
                }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                        /* User clicked Cancel */
                }
        })
        .show();
       }
};    

最佳答案

首先,这有点多余,因为 Android Market 会自动通知用户更新,但是,为了做到这一点,您需要一个服务器,在该服务器上放置一个 txt 文件,其中包含您的代码看起来的信息因为,在这种情况下,您的代码会查找版本代码。该代码在这里:

int curVersion = getPackageManager().getPackageInfo("your.app.id", 0).versionCode;
int newVersion = Integer.valueOf(s);

然后,您的应用必须将两者进行比较,如果 newVersion 大于 curVersion,则将市场打开到您的应用页面,用户可以在其中更新它。

您发布的代码完成了所有这些,但要更具体地回答您在 url 中的问题,您需要将 url 放入您的“currentVersion.txt 文件”或任何文件名。

希望我有所帮助!

关于android - 关于检查应用程序启动 android 更新的新手问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5211043/

有关android - 关于检查应用程序启动 android 更新的新手问题的更多相关文章

  1. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  2. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  3. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  4. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  5. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  6. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  7. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  8. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  9. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  10. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

随机推荐