android - 如何关闭或停止我在 Android 中启动的外部应用程序

标签 android

<分区>

我有一个应用程序需要显示一些 PDF 文件。这些文件位于设备本地,并且该设备用于无法访问 Web 的高安全性位置。

在 Android 获得一些 native 功能或库来显示 PDF 文件之前,我依赖于 Adob​​eReader.apk,因此我将其启动:

Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(dgfile), "application/pdf");
i.setClassName("com.adobe.reader", "com.adobe.reader.AdobeReader");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
// ctx is a passed-in Context of my main Activity
try   { 
    ctx.startActivity(i);
}
 catch (ActivityNotFoundException e)    {   // incase no viewer installed
    Log.e("DG_External", "No viewer found!");
}

这非常有效,但在我完成显示后我想停止它并返回到我的主应用程序。 this S.O question 上的“获胜”答案说如果我使用 StartActivityForresult() 我可以用 finishActivity() 但我无法让它工作(我不明白它是如何工作的因为没有我们要停止的 Activity 实例)

不过,我将 StartActivity 更改为:

((Activity)ctx).startActivityForResult(i, 33);  //!!  // 33 = arbitrary r.c.

这很好地调用了 Adob​​e 阅读器,但是做...

((Activity)ctx).finishActivity(33);

...没有产生任何结果。我尝试将它包装在 try/catch 中并且没有抛出任何异常。

我做错了什么,我该如何做对?提前致谢!

最佳答案

Another way to think about this is how can I bring my app to the top of the display and move AdobeReader to the background?

对您的一项 Activity 调用 startActivity()。您的应用程序中的某些内容正在确定“PC 上的用户关闭文档”。那个东西可以调用 startActivity()

关于android - 如何关闭或停止我在 Android 中启动的外部应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11439833/

相关文章:

php - Android 从数据库获取数据不起作用(php + json + mysql)

android - 警报广播类不再重复警报

android - soong/android.bp 构建是如何工作的?

android - ListView 错误和每部手机的行为不同

android - 应用填充后将 View 重置为默认值

安卓 : Force audio routing

android - 更改首选项 Activity 的外观

android - flutter 驱动程序 : Use Android back button

Android 应用程序的 Java 服务器

Android 在添加 spinnerSelector 时出错,找不到资源标识符