android 重启app

package com.xproject.utility;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class Restart {

    private Class<?> _unityPlayerClass;
    private Field _unityPlayerActivityField;
    public Activity _activity;
    void init()
    {
        try {
            this._unityPlayerClass =  Class.forName("com.unity3d.player.UnityPlayer");
            this._unityPlayerActivityField = this._unityPlayerClass.getField("currentActivity");
            //this._activity == null
            this._activity = (Activity) this._unityPlayerActivityField.get(this._unityPlayerClass);
        }
        catch (Exception e)
        {
            Log.d("Unity", e.toString());
        }
    }

    public void restartApplication() {
        init();
        new Thread(){
            public void run(){
                Intent launch=_activity.getBaseContext().getPackageManager().getLaunchIntentForPackage(
                        _activity.getBaseContext().getPackageName());
                launch.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                _activity.startActivity(launch);
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        }.start();
        _activity.finish();
    }

}

unity 中调用

private AndroidJavaClass javaObj = null;
void InitJavaTools()
{
        
    javaObj = new AndroidJavaClass("com.cytx.tools.helper");
    using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    {
        object jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        javaObj.CallStatic("init", jo);
    }
} void RestartApplication() { Debug.Log("restartApplication0"); javaObj.CallStatic("restartApplication"); Debug.Log("restartApplication2"); }
    void RestartApp()
    {
#if UNITY_ANDROID && !UNITY_EDITOR
        AndroidJavaClass jc = new AndroidJavaClass("com.luo.pc.mylibrary.Restart");
        AndroidJavaObject jo = jc.CallStatic<AndroidJavaObject>("getInstance", gameObject.name);
        jo.Call("restartApplication");
#else
        Debug.Log("restart");
#endif
       
    }

  

  

转载于:https://www.cnblogs.com/unity-android-ios/p/7880531.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值