参考this帖子,我需要问别的。
我有一个应用程序“A”,下载并安装另一个应用程序“B”。
我希望 B 将数据“转移”到 A ,然后 A 将使用此数据进行工作。
我知道我们可以使用intent
传输数据
使用 A 安装 B 应用后,Android会选择“确定”或“启动”;我的问题是:
我知道可能很难理解,您可以尝试检查我之前的抽奖(here)。
修改
我使用此代码从 A 启动 B 安装。
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString() + "/downloadedfile.apk"));
Intent.setDataAndType(uri, "application/vnd.android.package-archive");
getApplicationContext().startActivity(intent);
答案 0 :(得分:1)
有很多方法可以解决这个问题,这是(我相信)实现起来非常简单的方法。由于你的应用程序[推测]知道它正在安装什么:
应用程序A:添加BroadcastReceiver
以对安装作出反应,但默认情况下它已关闭。
Android: BroadcastReceiver on application install / uninstall
应用B:添加Service
进行后台通信。
注意:必须导出
Service
以便其他应用可以通过明确的意图访问,但这会产生安全问题,因为它对所有其他应用程序开放。
当App A的用户点击安装App B时:
使用过滤器集启动BroadcastReceiver
以检测安装:
stackoverflow...android-broadcastreceiver-on-application-install-uninstall
App A开始安装。
当BroadcastReceiver
检测到已添加包(包名称将在收到的意图中)时,它可以停止BroadcastReceiver,并可以在AppB中发送explicit Intent
命名服务。您可以在意图中传递所需的任何数据。
当AppB服务收到意图时,它可以以您喜欢的任何方式行事。
服务总是使用非空的Intent创建,尽管' action'显式Intent
s的值为null。
如果重新创建服务,Service.onStartCommand()可能会收到null Intent。
我填写了更多的代码,但我有一份日常工作;)
注意:
安装软件包时调用Intent.ACTION_PACKAGE_ADDED Intent.ACTION_PACKAGE_INSTALL从未使用过,在API 14中已弃用。
http://developer.android.com/reference/android/content/BroadcastReceiver.html http://developer.android.com/reference/android/content/Intent.html