【问题标题】:How to find out when an installation is completed如何知道安装何时完成
【发布时间】:2014-12-29 20:38:27
【问题描述】:

我正在创建一个应用程序来安装从服务器下载的应用程序。我想安装这些应用程序下载文件后,我用来安装的方法的代码在这里:

 public void Install(String name)
{
    //prompts user to accept any installation of the apk with provided name
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File
    (Environment.getExternalStorageDirectory() + "/ContentManager/" + name)), "application/vnd.android.package-archive");
    startActivity(intent);
    //this code should execute after the install finishes
    File file = new File(Environment.getExternalStorageDirectory() + "/ContentManager/"+name);
    file.delete();

}

我想在安装完成后从 sd 卡中删除 apk 文件。此代码在安装开始后将其删除,从而导致安装失败。我对android相当陌生,非常感谢一些帮助。在继续该过程之前,我基本上是在尝试等待安装完成。

【问题讨论】:

  • 这不是一个答案,而是更多地提醒您一种可能性,并可能为您节省时间和精力来处理可能不允许的事情。我不确定,也许这里的其他人可以确认,但我认为不允许从 Marketplace 以外的任何地方下载应用程序。
  • 已经有从我设置的私人服务器下载应用程序的代码,此安装代码有效,但之后apk仍然存在,我希望将其删除。

标签: android


【解决方案1】:

Android 包管理器会在安装(或更新/删除)应用程序时发送各种广播意图

您可以注册broadcast receivers,这样您就会收到通知,例如安装新应用程序时。

您可能感兴趣的意图是:

使用广播接收器没什么大不了的:

BroadcastReceiver myReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // do whatever you want to do
    }
};

registerReceiver(myReceiver, new IntentFilter("ACTION"));
unregisterReceiver(myReceiver);

【讨论】:

  • 这段代码似乎可以工作,但我似乎无法让广播接收器接收任何内容,我在主线程中对其进行了初始化,但似乎什么也没发生。任何帮助将不胜感激。
  • @Bmoore 您注册了哪些操作以及具体注册方式?
  • 我复制了该代码,并将其放在班级顶部,并将意图过滤器中的“ACTION”更改为“ACTION_PACKAGE_INSTALL”
  • @Bmoore 好的,您必须使用"android.intent.action.PACKAGE_INSTALL"Intent.ACTION_PACKAGE_INSTALL。顺便说一句,这是在开始安装时发送的 - ACTION_PACKAGE_ADDED 可能对你更好;)
  • PACKAGE_INSTALL 已弃用,请使用 package_added
【解决方案2】:

这可能不是最好的方法,但我解决了这个问题。这是我的方法的新代码。

 public void Install(final String name,View view)
{
    //prompts user to accept any installation of the apk with provided name
    printstatus("Installing apk please accept permissions");
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File
    (Environment.getExternalStorageDirectory() + "/ContentManager/" + name)), "application/vnd.android.package-archive");
    startActivity(intent);
    try {
        Thread.sleep(1500);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    for(int i=0;i<100;)
    {
        System.gc();
        if(view.getWindowVisibility()==0)
        {
            i=200;
            System.gc();
        }
        try {
            Thread.sleep(500);
            System.gc();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    File file = new File(Environment.getExternalStorageDirectory() + "/ContentManager/"+name);
    file.delete();
}

我创建了一个循环,它会等到窗口位于最前面,让方法继续执行。垃圾收集器和线程休眠可防止它减慢系统速度或 Linux 内核杀死进程。循环之前的睡眠是必需的,因此包管理器有时间在循环开始之前启动。

【讨论】:

  • 这里,为什么要从方法中传递视图变量?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-01
  • 2011-04-05
  • 2021-04-25
相关资源
最近更新 更多