Fix so Android app is fully closed when exiting

This commit is contained in:
drdev
2014-08-24 18:06:26 +00:00
parent 32cac7b12c
commit efa6b60b47
10 changed files with 100 additions and 59 deletions

View File

@@ -10,7 +10,8 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
<application
android:allowBackup="true"
@@ -19,11 +20,12 @@
android:largeHeap="true">
<activity
android:name=".Main"
android:label="@string/app_name" >
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</activity>
<activity android:name=".Exiter" android:theme="@android:style/Theme.NoDisplay"/>
</application>
</manifest>

View File

@@ -356,13 +356,6 @@
<include name="assets.zip" />
</fileset>
</ftp>
<ftp password="${cardforge.pass}" server="ftp.cardforge.org"
userid="${cardforge.user}" passive="true"
remotedir="releases/forge/forge-gui-android/">
<fileset dir="${project.build.directory}/classes">
<include name="version.txt" />
</fileset>
</ftp>
</target>
</configuration>
<goals>

View File

@@ -0,0 +1,14 @@
package forge.app;
import android.app.Activity;
import android.os.Bundle;
public class Exiter extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
finish();
//ensure process fully killed
System.exit(0);
}
}

View File

@@ -1,7 +1,6 @@
package forge.app;
import java.io.File;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
@@ -20,7 +19,6 @@ import com.badlogic.gdx.backends.android.AndroidApplication;
import forge.Forge;
import forge.interfaces.IDeviceAdapter;
import forge.util.Callback;
import forge.util.FileUtil;
public class Main extends AndroidApplication {
@@ -34,47 +32,34 @@ public class Main extends AndroidApplication {
this.setRequestedOrientation(7);
}
AndroidAdapter adapter = new AndroidAdapter();
//establish assets directory
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
Gdx.app.error("Forge", "Can't access external storage");
Gdx.app.exit();
adapter.exit();
return;
}
String assetsDir = Environment.getExternalStorageDirectory() + "/Forge/";
if (!FileUtil.ensureDirectoryExists(assetsDir)) {
Gdx.app.error("Forge", "Can't access external storage");
Gdx.app.exit();
adapter.exit();
return;
}
initialize(Forge.getApp(new AndroidClipboard(), new AndroidAdapter(),
assetsDir, new Callback<String>() {
@Override
public void run(String runOnExit) {
if (runOnExit != null) {
runFile(runOnExit);
}
//ensure process doesn't stick around after exiting
finish();
System.exit(0);
}
}));
initialize(Forge.getApp(new AndroidClipboard(), adapter, assetsDir));
}
private void runFile(String filename) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(filename));
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
MimeTypeMap.getFileExtensionFromUrl(uri.toString()));
intent.setDataAndType(uri, type);
startActivity(intent);
}
catch (Exception e) {
e.printStackTrace();
}
}
/*@Override
protected void onDestroy() {
super.onDestroy();
//ensure app doesn't stick around
//ActivityManager am = (ActivityManager)getSystemService(Activity.ACTIVITY_SERVICE);
//am.killBackgroundProcesses(getApplicationContext().getPackageName());
}*/
//special clipboard that words on Android
private class AndroidClipboard implements com.badlogic.gdx.utils.Clipboard {
@@ -118,5 +103,34 @@ public class Main extends AndroidApplication {
public String getDownloadsDir() {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
}
@Override
public boolean openFile(String filename) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(filename));
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
MimeTypeMap.getFileExtensionFromUrl(uri.toString()));
intent.setDataAndType(uri, type);
startActivity(intent);
return true;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public void exit() {
// Replace the current task with one that is excluded from the recent
// apps and that will finish itself immediately. It's critical that this
// activity get launched in the task that you want to hide.
final Intent relaunch = new Intent(Main.this, Exiter.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK // CLEAR_TASK requires this
| Intent.FLAG_ACTIVITY_CLEAR_TASK // finish everything else in the task
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); // hide (remove, in this case) task from recents
startActivity(relaunch);
}
}
}