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 }