android - 如何在当前 Activity 仍在启动时启动 native Activity

标签 android android-activity

我需要有关如何在当前 Activity 仍在启动时启动 native Activity 的建议。我阅读了 reference而且我不知道什么是获得我需要的东西的正确方法。

所以解释...

我需要启动原生视频 Activity 并根据某些条件播放一些视频。 因此,而不是以下内容:

1. User launch an app (main activity is started)
2. Welcome screen is displayed while the app is loading
3. App is ready & running

I need something like this:

1. User launch an app
1.1. Check If some video is available
1.2. Play the video 
1.3. Once the video is finished or user press the back key, continue to step 2.
2. Welcome screen is displayed while the app is loading
3. App is ready & running

My current attempt is based on the fact than an activity is paused as soon as some new activity is started so I'm simply starting my video activity in onResume call of the main activity:

public void onResume()
{
    super.onResume();
    if (someCondition && !videoIsPlayed)
    {
        videoIsPlayed = true;
        Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
        final Uri data = Uri.parse(videoURL);
        intent.setDataAndType(data, "video/mp4");
        startActivity(intent);
    }
}

但是,我没有发现任何证据表明上面的代码实际上是安全的......

我的另一个想法是引入两个类似于 Activity 选择器的 Activity ,在 onCreate 中我会选择开始哪个 Activity :

// Chooses which activity to start
public class ActivityChooser extends Activity
{
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        if (someCondition)
        {
            // start main application activity
        } else 
        {
            // start activity that plays the video using the native video activity
        }
        finish();
    }
}

还有另一个启动原生视频 Activity 的 Activity

// Starts the native video activity and once it finishes starts the main app activity
public class VideoPlayback extends Activity
{
    public void onResume()
    {
        super.onResume();
        if (someCondition && !videoIsPlayed)
        {
            videoIsPlayed = true;
            Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
            final Uri data = Uri.parse(videoURL);
            intent.setDataAndType(data, "video/mp4");
            startActivity(intent);

            // Question that remains here is how to know when the native video activity is done with
            // the playback in order to start main application activity
        }
    }
}

编辑:由于没有太多答案,请大家评论我的第一种方法。

欢迎提出任何建议!

提前致谢!

最佳答案

在 onResume() 中,您的 Activity 处于运行状态,并且在调用 onPause 之前将处于该状态。在此状态下,调用新 Activity 是安全的。您在 onResume 中的代码:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
final Uri data = Uri.parse(videoURL);
startActivity(intent);

很好(虽然显然你应该对 uri 做些什么)。这将导致主要 Activity 暂停并启动视频 Activity 。将它放在不同的 Activity 中基本上是不必要的;但是,由于您想知道视频何时结束,您可以在 VideoPlayback Activity 中使用 VideoView,而不是在新的 Intent 中启动它。然后,您可以附加一个 setOnCompletionListener (MediaPlayer.OnCompletionListener l),以便在视频完成时收到通知,并将结果返回给主要 Intent (它将使用 startActivityForResult 调用 VideoPlayback,并在 onActivityResult 中接收其完成通知。

关于android - 如何在当前 Activity 仍在启动时启动 native Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6901019/

相关文章:

java - Azure MobileService 返回 NullPointerException (Android)

android - 在 Android Studio 中不重构的情况下更改 Android 包名称

java - 无法在 Android 中以 AM、PM 格式正确显示时间

java - Android布局问题: Getting the Namespace Right

Android 应用程序标题栏停留在最近的应用程序 View 中

android - 使用可绘制对象动画图像

android - Unresolved 类 MainActivity - AndroidManifest.xml

android - 对这个 Android Activity 生命周期场景感到困惑 - 每当应用程序返回前台时都会调用 OnCreate

android - 在 list 文件中定义 Activity 的正确语法是什么

android - 如何保存 Activity 的状态?