Commit d69b698e authored by kenan's avatar kenan

游戏结束杀死自己进程

parent fe5870f0
...@@ -132,7 +132,7 @@ void DeckBuilder::Terminate() { ...@@ -132,7 +132,7 @@ void DeckBuilder::Terminate() {
//os::Printer::log("setLastDeck", linebuf); //os::Printer::log("setLastDeck", linebuf);
mainGame->SaveConfig(); mainGame->SaveConfig();
if(exit_on_return) if(exit_on_return)
mainGame->device->closeDevice(); mainGame->OnGameClose();
} }
bool DeckBuilder::OnEvent(const irr::SEvent& event) { bool DeckBuilder::OnEvent(const irr::SEvent& event) {
#ifdef _IRR_ANDROID_PLATFORM_ #ifdef _IRR_ANDROID_PLATFORM_
......
...@@ -679,7 +679,7 @@ void DuelClient::HandleSTOCPacketLan(char* data, unsigned int len) { ...@@ -679,7 +679,7 @@ void DuelClient::HandleSTOCPacketLan(char* data, unsigned int len) {
mainGame->SaveConfig(); mainGame->SaveConfig();
event_base_loopbreak(client_base); event_base_loopbreak(client_base);
if(exit_on_return) if(exit_on_return)
mainGame->device->closeDevice(); mainGame->OnGameClose();
break; break;
} }
case STOC_REPLAY: { case STOC_REPLAY: {
...@@ -980,7 +980,7 @@ int DuelClient::ClientAnalyze(char * msg, unsigned int len) { ...@@ -980,7 +980,7 @@ int DuelClient::ClientAnalyze(char * msg, unsigned int len) {
mainGame->gMutex.unlock(); mainGame->gMutex.unlock();
event_base_loopbreak(client_base); event_base_loopbreak(client_base);
if(exit_on_return) if(exit_on_return)
mainGame->device->closeDevice(); mainGame->OnGameClose();
} }
return false; return false;
} }
......
...@@ -145,7 +145,7 @@ bool ClientField::OnEvent(const irr::SEvent& event) { ...@@ -145,7 +145,7 @@ bool ClientField::OnEvent(const irr::SEvent& event) {
else else
mainGame->ShowElement(mainGame->wLanWindow); mainGame->ShowElement(mainGame->wLanWindow);
if(exit_on_return) if(exit_on_return)
mainGame->device->closeDevice(); mainGame->OnGameClose();
} else { } else {
mainGame->PopupElement(mainGame->wSurrender); mainGame->PopupElement(mainGame->wSurrender);
} }
......
...@@ -39,15 +39,6 @@ void Game::process(irr::SEvent &event) { ...@@ -39,15 +39,6 @@ void Game::process(irr::SEvent &event) {
} }
#ifdef _IRR_ANDROID_PLATFORM_ #ifdef _IRR_ANDROID_PLATFORM_
void onActivityLifeChanged(struct android_app* app, int32_t status){
//status ==
//ANDROID_ACTIVITY_RESUME 1
//ANDROID_ACTIVITY_STOP 2
// ANDROID_ACTIVITY_PAUSE 5
//ANDROID_ACTIVITY_DESTROY 3
//ANDROID_ACTIVITY_FOCUS 4
}
bool Game::Initialize(ANDROID_APP app, android::InitOptions *options) { bool Game::Initialize(ANDROID_APP app, android::InitOptions *options) {
this->appMain = app; this->appMain = app;
#endif #endif
...@@ -81,8 +72,6 @@ bool Game::Initialize(ANDROID_APP app, android::InitOptions *options) { ...@@ -81,8 +72,6 @@ bool Game::Initialize(ANDROID_APP app, android::InitOptions *options) {
device->setProcessReceiver(this); device->setProcessReceiver(this);
app->onInputEvent = android::handleInput; app->onInputEvent = android::handleInput;
((CIrrDeviceAndroid*)device)->onActivityLifeChanged = onActivityLifeChanged;
ILogger* logger = device->getLogger(); ILogger* logger = device->getLogger();
// logger->setLogLevel(ELL_WARNING); // logger->setLogLevel(ELL_WARNING);
isPSEnabled = options->isPendulumScaleEnabled(); isPSEnabled = options->isPendulumScaleEnabled();
...@@ -2028,4 +2017,8 @@ void Game::ChangeToIGUIImageButton(irr::gui::IGUIButton* button, irr::video::ITe ...@@ -2028,4 +2017,8 @@ void Game::ChangeToIGUIImageButton(irr::gui::IGUIButton* button, irr::video::ITe
button->setOverrideFont(font); button->setOverrideFont(font);
} }
void Game::OnGameClose() {
android::onGameExit(appMain);
this->device->closeDevice();
} }
}
\ No newline at end of file
...@@ -167,11 +167,12 @@ public: ...@@ -167,11 +167,12 @@ public:
void AddDebugMsg(const char* msgbuf); void AddDebugMsg(const char* msgbuf);
void ErrorLog(const char* msgbuf); void ErrorLog(const char* msgbuf);
void addMessageBox(const wchar_t* caption, const wchar_t* text); void addMessageBox(const wchar_t* caption, const wchar_t* text);
void initUtils(); void initUtils(){}
void ClearTextures(); void ClearTextures();
void CloseGameButtons(); void CloseGameButtons();
void CloseGameWindow(); void CloseGameWindow();
void CloseDuelWindow(); void CloseDuelWindow();
void OnGameClose();
void ChangeToIGUIImageWindow(irr::gui::IGUIWindow* window, irr::gui::IGUIImage* bgwindow, irr::video::ITexture* image); void ChangeToIGUIImageWindow(irr::gui::IGUIWindow* window, irr::gui::IGUIImage* bgwindow, irr::video::ITexture* image);
void ChangeToIGUIImageButton(irr::gui::IGUIButton* button, irr::video::ITexture* image, irr::video::ITexture* pressedImage, irr::gui::CGUITTFont* font=0); void ChangeToIGUIImageButton(irr::gui::IGUIButton* button, irr::video::ITexture* image, irr::video::ITexture* pressedImage, irr::gui::CGUITTFont* font=0);
......
...@@ -65,6 +65,7 @@ int main(int argc, char* argv[]) { ...@@ -65,6 +65,7 @@ int main(int argc, char* argv[]) {
bool keep_on_return = false; bool keep_on_return = false;
bool open_file = false; bool open_file = false;
#ifdef _IRR_ANDROID_PLATFORM_ #ifdef _IRR_ANDROID_PLATFORM_
__android_log_print(ANDROID_LOG_WARN, "ygo", "handle args %d", argc);
//android //android
for(int i = 0; i < argc; ++i) { for(int i = 0; i < argc; ++i) {
const char* arg = argv[i].c_str(); const char* arg = argv[i].c_str();
...@@ -77,7 +78,6 @@ int main(int argc, char* argv[]) { ...@@ -77,7 +78,6 @@ int main(int argc, char* argv[]) {
wchar_t fname[1024]; wchar_t fname[1024];
char* tmp = sub_string(arg, 2); char* tmp = sub_string(arg, 2);
BufferIO::DecodeUTF8(tmp, fname); BufferIO::DecodeUTF8(tmp, fname);
__android_log_print(ANDROID_LOG_DEBUG, "ygo", "load cdb=%s", tmp);
ygo::dataManager.LoadDB(fname); ygo::dataManager.LoadDB(fname);
delete tmp; delete tmp;
} else if(!strcmp(arg, "-k")) { // Keep on return } else if(!strcmp(arg, "-k")) { // Keep on return
......
...@@ -63,7 +63,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) { ...@@ -63,7 +63,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) {
case BUTTON_MODE_EXIT: { case BUTTON_MODE_EXIT: {
mainGame->soundManager->StopBGM(); mainGame->soundManager->StopBGM();
mainGame->SaveConfig(); mainGame->SaveConfig();
mainGame->device->closeDevice(); mainGame->OnGameClose();
break; break;
} }
case BUTTON_LAN_MODE: { case BUTTON_LAN_MODE: {
...@@ -121,7 +121,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) { ...@@ -121,7 +121,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) {
mainGame->HideElement(mainGame->wLanWindow); mainGame->HideElement(mainGame->wLanWindow);
mainGame->ShowElement(mainGame->wMainMenu); mainGame->ShowElement(mainGame->wMainMenu);
if(exit_on_return) if(exit_on_return)
mainGame->device->closeDevice(); mainGame->OnGameClose();
break; break;
} }
case BUTTON_LAN_REFRESH: { case BUTTON_LAN_REFRESH: {
...@@ -214,7 +214,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) { ...@@ -214,7 +214,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) {
mainGame->wChat->setVisible(false); mainGame->wChat->setVisible(false);
mainGame->SaveConfig(); mainGame->SaveConfig();
if(exit_on_return) if(exit_on_return)
mainGame->device->closeDevice(); mainGame->OnGameClose();
break; break;
} }
case BUTTON_REPLAY_MODE: { case BUTTON_REPLAY_MODE: {
......
...@@ -249,7 +249,7 @@ void ReplayMode::EndDuel() { ...@@ -249,7 +249,7 @@ void ReplayMode::EndDuel() {
mainGame->device->setEventReceiver(&mainGame->menuHandler); mainGame->device->setEventReceiver(&mainGame->menuHandler);
mainGame->gMutex.unlock(); mainGame->gMutex.unlock();
if(exit_on_return) if(exit_on_return)
mainGame->device->closeDevice(); mainGame->OnGameClose();
} }
} }
void ReplayMode::Restart(bool refresh) { void ReplayMode::Restart(bool refresh) {
......
...@@ -154,7 +154,7 @@ int SingleMode::SinglePlayThread() { ...@@ -154,7 +154,7 @@ int SingleMode::SinglePlayThread() {
mainGame->gMutex.unlock(); mainGame->gMutex.unlock();
mainGame->SaveConfig(); mainGame->SaveConfig();
if(exit_on_return) if(exit_on_return)
mainGame->device->closeDevice(); mainGame->OnGameClose();
} }
return 0; return 0;
} }
......
...@@ -279,36 +279,21 @@ void CIrrDeviceAndroid::handleAndroidCommand(ANDROID_APP app, int32_t cmd) ...@@ -279,36 +279,21 @@ void CIrrDeviceAndroid::handleAndroidCommand(ANDROID_APP app, int32_t cmd)
case APP_CMD_LOST_FOCUS: case APP_CMD_LOST_FOCUS:
os::Printer::log("Android command APP_CMD_LOST_FOCUS", ELL_DEBUG); os::Printer::log("Android command APP_CMD_LOST_FOCUS", ELL_DEBUG);
device->Focused = false; device->Focused = false;
if(device->onActivityLifeChanged != nullptr){
device->onActivityLifeChanged(app, ANDROID_ACTIVITY_FOCUS);
}
break; break;
case APP_CMD_DESTROY: case APP_CMD_DESTROY:
os::Printer::log("Android command APP_CMD_DESTROY", ELL_DEBUG); os::Printer::log("Android command APP_CMD_DESTROY", ELL_DEBUG);
device->Initialized = false; device->Initialized = false;
if(device->onActivityLifeChanged != nullptr){
device->onActivityLifeChanged(app, ANDROID_ACTIVITY_DESTROY);
}
break; break;
case APP_CMD_PAUSE: case APP_CMD_PAUSE:
os::Printer::log("Android command APP_CMD_PAUSE", ELL_DEBUG); os::Printer::log("Android command APP_CMD_PAUSE", ELL_DEBUG);
device->Paused = true; device->Paused = true;
if(device->onActivityLifeChanged != nullptr){
device->onActivityLifeChanged(app, ANDROID_ACTIVITY_PAUSE);
}
break; break;
case APP_CMD_STOP: case APP_CMD_STOP:
os::Printer::log("Android command APP_CMD_STOP", ELL_DEBUG); os::Printer::log("Android command APP_CMD_STOP", ELL_DEBUG);
if(device->onActivityLifeChanged != nullptr){
device->onActivityLifeChanged(app, ANDROID_ACTIVITY_STOP);
}
break; break;
case APP_CMD_RESUME: case APP_CMD_RESUME:
os::Printer::log("Android command APP_CMD_RESUME", ELL_DEBUG); os::Printer::log("Android command APP_CMD_RESUME", ELL_DEBUG);
device->Paused = false; device->Paused = false;
if(device->onActivityLifeChanged != nullptr){
device->onActivityLifeChanged(app, ANDROID_ACTIVITY_RESUME);
}
break; break;
case APP_CMD_WINDOW_RESIZED: case APP_CMD_WINDOW_RESIZED:
os::Printer::log("Android command APP_CMD_WINDOW_RESIZED", ELL_DEBUG); os::Printer::log("Android command APP_CMD_WINDOW_RESIZED", ELL_DEBUG);
......
...@@ -17,12 +17,6 @@ ...@@ -17,12 +17,6 @@
#include <android/sensor.h> #include <android/sensor.h>
#include <android_native_app_glue.h> #include <android_native_app_glue.h>
#define ANDROID_ACTIVITY_RESUME 1
#define ANDROID_ACTIVITY_STOP 2
#define ANDROID_ACTIVITY_PAUSE 5
#define ANDROID_ACTIVITY_DESTROY 3
#define ANDROID_ACTIVITY_FOCUS 4
namespace irr namespace irr
{ {
class CIrrDeviceAndroid : public CIrrDeviceStub, video::IImagePresenter class CIrrDeviceAndroid : public CIrrDeviceStub, video::IImagePresenter
...@@ -58,8 +52,6 @@ namespace irr ...@@ -58,8 +52,6 @@ namespace irr
virtual void restoreWindow(); virtual void restoreWindow();
void (*onActivityLifeChanged)(struct android_app* app, int32_t status);
virtual core::position2di getWindowPosition(); virtual core::position2di getWindowPosition();
virtual E_DEVICE_TYPE getType() const; virtual E_DEVICE_TYPE getType() const;
......
...@@ -887,6 +887,20 @@ void process_input(ANDROID_APP app, ...@@ -887,6 +887,20 @@ void process_input(ANDROID_APP app,
} }
} }
void onGameExit(ANDROID_APP app){
if (!app || !app->activity || !app->activity->vm)
return;
JNIEnv* jni = nullptr;
app->activity->vm->AttachCurrentThread(&jni, NULL);
jobject lNativeActivity = app->activity->clazz;
jclass ClassNativeActivity = jni->GetObjectClass(lNativeActivity);
jmethodID methodId = jni->GetMethodID(ClassNativeActivity,
"onGameExit", "()V");
jni->CallVoidMethod(lNativeActivity, methodId);
jni->DeleteLocalRef(ClassNativeActivity);
app->activity->vm->DetachCurrentThread();
}
s32 handleInput(ANDROID_APP app, AInputEvent* androidEvent) { s32 handleInput(ANDROID_APP app, AInputEvent* androidEvent) {
IrrlichtDevice* device = (IrrlichtDevice*) app->userData; IrrlichtDevice* device = (IrrlichtDevice*) app->userData;
s32 Status = 0; s32 Status = 0;
......
...@@ -193,6 +193,8 @@ extern s32 handleInput(ANDROID_APP app, AInputEvent* androidEvent); ...@@ -193,6 +193,8 @@ extern s32 handleInput(ANDROID_APP app, AInputEvent* androidEvent);
extern bool android_deck_delete(const char* deck_name); extern bool android_deck_delete(const char* deck_name);
extern void onGameExit(ANDROID_APP app);
extern void runWindbot(ANDROID_APP app, const char* args); extern void runWindbot(ANDROID_APP app, const char* args);
} }
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
android:value="YGOMobile"/> android:value="YGOMobile"/>
</activity> </activity>
<receiver android:name="cn.garymb.ygomobile.GameReceiver" <receiver android:name="cn.garymb.ygomobile.GameReceiver"
android:process=":game" > android:process=":gamehelper" >
<intent-filter> <intent-filter>
<action android:name="cn.garymb.ygomobile.game.start"/> <action android:name="cn.garymb.ygomobile.game.start"/>
<action android:name="cn.garymb.ygomobile.game.stop"/> <action android:name="cn.garymb.ygomobile.game.stop"/>
......
...@@ -17,6 +17,7 @@ import android.os.Build; ...@@ -17,6 +17,7 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.Process;
import android.util.Log; import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.HapticFeedbackConstants; import android.view.HapticFeedbackConstants;
...@@ -544,4 +545,10 @@ public class YGOMobileActivity extends NativeActivity implements ...@@ -544,4 +545,10 @@ public class YGOMobileActivity extends NativeActivity implements
} }
}); });
} }
@Override
public void onGameExit() {
Log.e("ygomobile", "game exit");
Process.killProcess(Process.myPid());
}
} }
...@@ -213,5 +213,7 @@ public final class IrrlichtBridge { ...@@ -213,5 +213,7 @@ public final class IrrlichtBridge {
int getPositionX(); int getPositionX();
int getPositionY(); int getPositionY();
void onGameExit();
} }
} }
...@@ -13,7 +13,7 @@ android { ...@@ -13,7 +13,7 @@ android {
flavorDimensions "versionCode" flavorDimensions "versionCode"
vectorDrawables.useSupportLibrary = true vectorDrawables.useSupportLibrary = true
ndk { ndk {
abiFilters "arm64-v8a", "armeabi-v7a", "x86" abiFilters "armeabi-v7a"//"arm64-v8a", "armeabi-v7a", "x86"
} }
} }
compileOptions { compileOptions {
...@@ -34,6 +34,9 @@ android { ...@@ -34,6 +34,9 @@ android {
} }
} }
buildTypes { buildTypes {
debug {
debuggable false
}
release { release {
shrinkResources false shrinkResources false
minifyEnabled false minifyEnabled false
......
...@@ -37,6 +37,7 @@ public class GameUriManager { ...@@ -37,6 +37,7 @@ public class GameUriManager {
} }
public boolean doIntent(Intent intent) { public boolean doIntent(Intent intent) {
Log.i("ygo", "doIntent");
if (ACTION_OPEN_DECK.equals(intent.getAction())) { if (ACTION_OPEN_DECK.equals(intent.getAction())) {
if (intent.getData() != null) { if (intent.getData() != null) {
doUri(intent.getData()); doUri(intent.getData());
...@@ -119,6 +120,7 @@ public class GameUriManager { ...@@ -119,6 +120,7 @@ public class GameUriManager {
private void doUri(Uri uri) { private void doUri(Uri uri) {
Intent startSeting = new Intent(activity, SettingsActivity.class); Intent startSeting = new Intent(activity, SettingsActivity.class);
Log.i("ygo", "doUri:"+uri);
if ("file".equals(uri.getScheme())) { if ("file".equals(uri.getScheme())) {
File file = new File(uri.getPath()); File file = new File(uri.getPath());
if (file.getName().toLowerCase(Locale.US).endsWith(".ydk")) { if (file.getName().toLowerCase(Locale.US).endsWith(".ydk")) {
...@@ -162,9 +164,11 @@ public class GameUriManager { ...@@ -162,9 +164,11 @@ public class GameUriManager {
Toast.makeText(activity, activity.getString(R.string.install_failed_bcos) + e, Toast.LENGTH_LONG).show(); Toast.makeText(activity, activity.getString(R.string.install_failed_bcos) + e, Toast.LENGTH_LONG).show();
} }
} }
if (!ComponentUtils.isActivityRunning(getActivity(), new ComponentName(getActivity(), YGOMobileActivity.class))) { if (!YGOStarter.isGameRunning(getActivity())) {
YGOStarter.startGame(getActivity(), null, "-r", yrp.getName()); YGOStarter.startGame(getActivity(), null, "-r", yrp.getName());
Toast.makeText(activity, ""+yrp.getName(), Toast.LENGTH_LONG).show(); Toast.makeText(activity, ""+yrp.getName(), Toast.LENGTH_LONG).show();
} else {
Log.w("ygo", "game is running");
} }
} }
} else if ("content".equals(uri.getScheme())) { } else if ("content".equals(uri.getScheme())) {
...@@ -242,9 +246,11 @@ public class GameUriManager { ...@@ -242,9 +246,11 @@ public class GameUriManager {
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
} }
if (!ComponentUtils.isActivityRunning(activity, new ComponentName(activity, YGOMobileActivity.class))) { if (!YGOStarter.isGameRunning(getActivity())) {
YGOStarter.startGame(getActivity(), null, "-r ", yrp.getName()); YGOStarter.startGame(getActivity(), null, "-r", yrp.getName());
Toast.makeText(activity, activity.getString(R.string.yrp_installed), Toast.LENGTH_LONG).show(); Toast.makeText(activity, activity.getString(R.string.yrp_installed), Toast.LENGTH_LONG).show();
} else {
Log.w("ygo", "game is running");
} }
} }
} else { } else {
......
...@@ -3,6 +3,8 @@ package cn.garymb.ygomobile; ...@@ -3,6 +3,8 @@ package cn.garymb.ygomobile;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.graphics.Bitmap; import android.graphics.Bitmap;
...@@ -19,6 +21,7 @@ import com.bumptech.glide.Glide; ...@@ -19,6 +21,7 @@ import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.ViewTarget; import com.bumptech.glide.request.target.ViewTarget;
import com.bumptech.glide.signature.StringSignature; import com.bumptech.glide.signature.StringSignature;
import com.tencent.bugly.proguard.C;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
...@@ -27,6 +30,7 @@ import cn.garymb.ygodata.YGOGameOptions; ...@@ -27,6 +30,7 @@ import cn.garymb.ygodata.YGOGameOptions;
import cn.garymb.ygomobile.core.IrrlichtBridge; import cn.garymb.ygomobile.core.IrrlichtBridge;
import cn.garymb.ygomobile.lite.R; import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.ui.plus.ViewTargetPlus; import cn.garymb.ygomobile.ui.plus.ViewTargetPlus;
import cn.garymb.ygomobile.utils.ComponentUtils;
public class YGOStarter { public class YGOStarter {
...@@ -207,4 +211,9 @@ public class YGOStarter { ...@@ -207,4 +211,9 @@ public class YGOStarter {
int oldRequestedOrientation; int oldRequestedOrientation;
boolean isRunning = false; boolean isRunning = false;
} }
public static boolean isGameRunning(Context context) {
return ComponentUtils.isProcessRunning(context, context.getPackageName() + ":game")
&& ComponentUtils.isActivityRunning(context, new ComponentName(context, YGOMobileActivity.class));
}
} }
...@@ -435,7 +435,7 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie ...@@ -435,7 +435,7 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie
builder.setLeftButtonText(R.string.join_game); builder.setLeftButtonText(R.string.join_game);
builder.setLeftButtonListener((dlg, i) -> { builder.setLeftButtonListener((dlg, i) -> {
dlg.dismiss(); dlg.dismiss();
if (Build.VERSION.SDK_INT >= 23 && ComponentUtils.isActivityRunning(this, new ComponentName(this, YGOMobileActivity.class))) { if (Build.VERSION.SDK_INT >= 23 && YGOStarter.isGameRunning(getActivity())) {
Toast toast = Toast.makeText(getApplicationContext(), R.string.tip_return_to_duel, Toast.LENGTH_SHORT); Toast toast = Toast.makeText(getApplicationContext(), R.string.tip_return_to_duel, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0); toast.setGravity(Gravity.CENTER, 0, 0);
toast.show(); toast.show();
...@@ -558,7 +558,7 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie ...@@ -558,7 +558,7 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie
tv.setOnClickListener((v) -> { tv.setOnClickListener((v) -> {
openGame(); openGame();
}); });
if (ComponentUtils.isActivityRunning(this, new ComponentName(this, YGOMobileActivity.class))) { if (YGOStarter.isGameRunning(getActivity())) {
tv.setVisibility(View.VISIBLE); tv.setVisibility(View.VISIBLE);
} else { } else {
tv.setVisibility(View.GONE); tv.setVisibility(View.GONE);
...@@ -664,7 +664,7 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie ...@@ -664,7 +664,7 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie
} }
public void showTipsToast() { public void showTipsToast() {
if (!ComponentUtils.isActivityRunning(this, new ComponentName(this, YGOMobileActivity.class))) { if (!YGOStarter.isGameRunning(getActivity())) {
String[] tipsList = this.getResources().getStringArray(R.array.tips); String[] tipsList = this.getResources().getStringArray(R.array.tips);
int x = (int) (Math.random() * tipsList.length); int x = (int) (Math.random() * tipsList.length);
String tips = tipsList[x]; String tips = tipsList[x];
......
...@@ -196,7 +196,7 @@ public class MainActivity extends HomeActivity { ...@@ -196,7 +196,7 @@ public class MainActivity extends HomeActivity {
super.onResume(); super.onResume();
YGOStarter.onResumed(this); YGOStarter.onResumed(this);
//如果游戏Activity已经不存在了,则 //如果游戏Activity已经不存在了,则
if (!ComponentUtils.isActivityRunning(this, new ComponentName(this, YGOMobileActivity.class))) { if (!YGOStarter.isGameRunning(getActivity())) {
sendBroadcast(new Intent(IrrlichtBridge.ACTION_STOP).setPackage(getPackageName())); sendBroadcast(new Intent(IrrlichtBridge.ACTION_STOP).setPackage(getPackageName()));
} }
} }
......
...@@ -4,12 +4,25 @@ import android.app.ActivityManager; ...@@ -4,12 +4,25 @@ import android.app.ActivityManager;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.text.TextUtils;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import java.util.List; import java.util.List;
public class ComponentUtils { public class ComponentUtils {
public static boolean isProcessRunning(Context context, String processName) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
if (tasks != null) {
for (ActivityManager.RunningAppProcessInfo taskInfo : tasks) {
if (context.getApplicationInfo().uid == taskInfo.uid && TextUtils.equals(processName, taskInfo.processName)) {
return true;
}
}
}
return false;
}
public static boolean isActivityRunning(Context context, ComponentName componentName) { public static boolean isActivityRunning(Context context, ComponentName componentName) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
return isActivityRunningV21(context, componentName); return isActivityRunningV21(context, componentName);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment