Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

安卓重新启动App 会有5s左右延迟, 有减少延迟的解决方案吗? #2

Open
VincentGogo opened this issue Jul 15, 2019 · 8 comments

Comments

@VincentGogo
Copy link

No description provided.

@noodle1983
Copy link
Owner

说的可是这段
`

    using (AndroidJavaClass unity_player = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    {
        AndroidJavaObject current_activity = unity_player.GetStatic<AndroidJavaObject>("currentActivity");

        AndroidJavaObject pm = current_activity.Call<AndroidJavaObject>("getPackageManager");
        AndroidJavaObject intent = pm.Call<AndroidJavaObject>("getLaunchIntentForPackage", Application.identifier);
        intent.Call<AndroidJavaObject>("setFlags", 0x20000000);//Intent.FLAG_ACTIVITY_SINGLE_TOP

        AndroidJavaClass pending_intent = new AndroidJavaClass("android.app.PendingIntent");
        AndroidJavaObject content_intent = pending_intent.CallStatic<AndroidJavaObject>("getActivity", current_activity, 0, intent, 0x8000000); //PendingIntent.FLAG_UPDATE_CURRENT = 134217728 [0x8000000]
        AndroidJavaObject alarm_manager = current_activity.Call<AndroidJavaObject>("getSystemService", "alarm");
        AndroidJavaClass system = new AndroidJavaClass("java.lang.System");
        long current_time = system.CallStatic<long>("currentTimeMillis");
        alarm_manager.Call("set", 1, current_time + 1000, content_intent); // android.app.AlarmManager.RTC = 1 [0x1]

        Debug.LogError("alarm_manager set time " + current_time + 1000);
        current_activity.Call("finish");

        AndroidJavaClass process = new AndroidJavaClass("android.os.Process");
        int pid = process.CallStatic<int>("myPid");
        process.CallStatic("killProcess", pid);
    }

`
我们用这个用了很久了, 如果有更好的方案, 愿闻其详

@VincentGogo
Copy link
Author

VincentGogo commented Jul 16, 2019 via email

@noodle1983
Copy link
Owner

alarm_manager可以减个几百毫秒, 但是不顶什么事

@DesperateZero
Copy link

	Intent intent = new Intent(activity, MainActivity.class);
	intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
			| Intent.FLAG_ACTIVITY_CLEAR_TASK
			| Intent.FLAG_ACTIVITY_NEW_TASK);
	PendingIntent pendingIntent = PendingIntent.getActivity(MApplication.getInstance().getBaseContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
	AlarmManager mgr = (AlarmManager) MApplication.getInstance().getBaseContext().getSystemService(Context.ALARM_SERVICE);

	long alarmTimeMillis = System.currentTimeMillis() + 1000;

	// pendingIntent 为发送广播
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
		mgr.setExactAndAllowWhileIdle(AlarmManager.RTC, alarmTimeMillis, pendingIntent);
	} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
		mgr.setExact(AlarmManager.RTC, alarmTimeMillis, pendingIntent);
	} else {
		mgr.set(AlarmManager.RTC, alarmTimeMillis, pendingIntent);
	}
	activity.finish();
	System.exit(0);

实测这样一秒是可以的 安卓原生代码

@chiuan
Copy link

chiuan commented Aug 5, 2019

确实等一会很难受。

@noodle1983
Copy link
Owner

只是改了intent的flag?
我试试

@sisong
Copy link

sisong commented Oct 16, 2019

为什么是1000ms,而不能是100ms?

@moonalong
Copy link

moonalong commented Dec 30, 2020

c# 那边调用android的这个方法可以秒启动:

`public void startRun()
{
new Thread()
{
public void run()
{
Intent localIntent = getApplicationContext().getPackageManager().getLaunchIntentForPackage(getApplicationContext().getPackageName());
localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
getApplicationContext().startActivity(localIntent);
android.os.Process.killProcess(android.os.Process.myPid());
}
}.start();

    finish();
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants