android - 为什么从Service中开启Activity有时会有延迟?
PHPz
PHPz 2017-04-17 17:56:33
0
4
424

我在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秒都有可能.这是为什么?

PHPz
PHPz

学习是最好的投资!

reply all(4)
阿神

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();
    }
PHPzhong

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!

伊谢尔伦

Change your launchMode to default and try it

大家讲道理

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template