search

Home  >  Q&A  >  body text

android - 为什么从Service中开启Activity有时会有延迟?

我在Service中定义了一个方法,在方法中

  Intent intent = new Intent(getApplicationContext(), SmokeActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);

想要开启一个Activity,这是这个Activity的设置

  <activity
            android:name="com.aa.safe.safehei.activities.SmokeActivity"
            android:launchMode="singleInstance"
            android:theme="@style/MyTheme">
        </activity>

我如果在这个Activity相同亲和性的Activity的窗口之上打开,那么它就会立即被调用,但是如果我返回到桌面后再使用服务中的方法开启这个Activity,他的响应效率就很差劲,延迟3-5秒都有可能.这是为什么?

PHPzPHPz2890 days ago460

reply all(4)I'll reply

  • 阿神

    阿神2017-04-17 17:58:33

    Are you monitoring the Home button to start the Activity? , there will indeed be a 5s delay. You can solve this problem with the following codeonReceive()中调用startActivity()

        Intent intent = new Intent(context, AnotherActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        try {
            pendingIntent.send(); // 监听到Home键按下后立即调用startActivity启动Activity会有5s延迟
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }

    reply
    0
  • PHPz

    PHPz2017-04-17 17:58:33

    It may have something to do with your opening method, because you opened it in SingleInstance Mode, and whether your Service is a remote Service!

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:58:33

    Change your launchMode to default and try it

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 17:58:33

    Can you post the code of your Service and SmokeActivity? From your description, I can’t confirm which part has the problem

    reply
    0
  • Cancelreply