-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnity里面调用Android重启应用
127 lines (111 loc) · 4.82 KB
/
Unity里面调用Android重启应用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.jianwu.tafang;
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 static final String TAG = "restart";
private static Restart _instance;
private Class<?> _unityPlayerClass;
private Field _unityPlayerActivityField;
private Method _unitySendMessageMethod;
public Activity _activity;
private String gameObject;
public static Restart getInstanceInActivity() {
return _instance;
}
public static Restart getInstance(String gameObject) {
if (_instance == null) {
_instance = new Restart( gameObject);
}
return _instance;
}
private Restart(String gameObject)
{
this.gameObject = gameObject;
try {
this._unityPlayerClass = Class.forName("com.unity3d.player.UnityPlayer");
this._unityPlayerActivityField = this._unityPlayerClass
.getField("currentActivity");
this._unitySendMessageMethod = this._unityPlayerClass.getMethod(
"UnitySendMessage", new Class[] { String.class,
String.class, String.class });
} catch (ClassNotFoundException e) {
Log.i(TAG, "could not find UnityPlayer class: " + e.getMessage());
} catch (NoSuchFieldException e) {
Log.i(TAG,
"could not find currentActivity field: " + e.getMessage());
} catch (NoSuchMethodException e) {
Log.i(TAG, "could not find UnityPlayer class: " + e.getMessage());
} catch (Exception e) {
Log.i(TAG,
"unkown exception occurred locating getActivity(): "
+ e.getMessage());
}
getActivity();
}
public Activity getActivity() {
if(this._activity == null) {
try {
this._activity = (Activity) this._unityPlayerActivityField.get(this._unityPlayerClass);
} catch (IllegalArgumentException e) {
Log.i(TAG, "error getting currentActivity: " + e.getMessage());
} catch (IllegalAccessException e) {
Log.i(TAG, "error getting currentActivity: " + e.getMessage());
}
}
return this._activity;
}
public void UnitySendMessage(String m, String p) {
// UnityPlayer.UnitySendMessage(gameObject, m, p);
if (this._unitySendMessageMethod != null) {
try {
this._unitySendMessageMethod.invoke(null, new Object[] {
gameObject, m, p });
} catch (IllegalArgumentException e) {
Log.i(TAG,
"could not find UnitySendMessage method: "
+ e.getMessage());
} catch (IllegalAccessException e) {
Log.i(TAG,
"could not find UnitySendMessage method: "
+ e.getMessage());
} catch (InvocationTargetException e) {
Log.i(TAG,
"could not find UnitySendMessage method: "
+ e.getMessage());
}
} else {
Log.i(TAG, "UnitySendMessage: " + gameObject + ", " + m + ", " + p);
}
}
public void restart() {
Intent restartIntent = getActivity().getPackageManager()
.getLaunchIntentForPackage(getActivity().getPackageName() );
PendingIntent intent = PendingIntent.getActivity(
getActivity(), 0,
restartIntent, Intent.FLAG_ACTIVITY_CLEAR_TOP);
AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
manager.set(AlarmManager.RTC, System.currentTimeMillis()+100, intent);
getActivity().finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
public void restart(int delay) {
if(delay <= 0 )
delay = 10000;
Intent restartIntent = getActivity().getPackageManager()
.getLaunchIntentForPackage(getActivity().getPackageName() );
PendingIntent intent = PendingIntent.getActivity(
getActivity(), 0,
restartIntent, Intent.FLAG_ACTIVITY_CLEAR_TOP);
AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
manager.set(AlarmManager.RTC, System.currentTimeMillis()+delay, intent);
getActivity().finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
}