在可序列化 class 中获取 Activity 的上下文
Get the context of Activity in a Serializable class
我有一个实现 'Serializable' 的 class,但我无法在其中获取我的主要 activity 的上下文。
这是我的代码 class:
public class Game implements Serializable{
private String name;
private ColorTheme theme;
private int distance = 0;
private int score = 0;
boolean boom = false;
protected Context context;
public Game(MainActivity context){
this.context = context.getApplicationContext();
}
在此之后我认为我得到了上下文然后我想要的是当分数大于 1 时应用程序应该关闭。
score = (i+1);
if (score >1)
{
}
我想在这种情况下关闭我的游戏,但它没有给我任何结束上下文的选项。请在我错的地方帮助我。我认为这个 Serializable class 中的上下文没有成功实现。需要帮助
仅供参考
在游戏 class 中关闭应用程序不是一个好方法,最好在 Activity 本身中进行。
一个很好的解决方案是在游戏 class 中创建一个方法,如下所示:
public boolean shouldCloseApplication(){
return this.score > 1;
}
并在 Activity 中使用以下内容:
if(game.shouldCloseApplication()){
//close the application as mentioned below
this.finishAffinity();
}
说,如果你想关闭应用程序,你只需要调用
this.finishAffinity();
refer to this SO post.
所以,不要传递“MainActivity
”,而是直接使用传递它的“Context
”
Game game = new Game(MainActivity.this);
并通过
接收
public Game(Context context){
this.context = context;
}
并与
一起使用
if(score > 1)
{
context.finishAffinity();
}
不建议,但有效
如果这不起作用,请使用
context.getActivity().finish();
System.exit(0);
希望对您有所帮助
试试这个
public Game(MainActivity context){
this.context = context;
}
score = (i+1);
if (score >1){
PackageManager pm = context.getPackageManager();
Intent i = new Intent("android.intent.action.MAIN");
i.addCategory("android.intent.category.HOME");
List<ResolveInfo> lst = pm.queryIntentActivities(i, 0);
if (lst != null) {
for (ResolveInfo resolveInfo : lst) {
try {
ApplicationInfo ai = pm.getApplicationInfo(
resolveInfo.activityInfo.packageName,0);
if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
Intent goHome = new Intent();
goHome.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
goHome.setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name);
context.startActivity(goHome);
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
break;
}
} catch (NameNotFoundException e) {
logMessage(LOG_TYPE_ERROR, "AppConstant-exitFromApp-kill", e.getMessage());
}
}
}
}
我有一个实现 'Serializable' 的 class,但我无法在其中获取我的主要 activity 的上下文。 这是我的代码 class:
public class Game implements Serializable{
private String name;
private ColorTheme theme;
private int distance = 0;
private int score = 0;
boolean boom = false;
protected Context context;
public Game(MainActivity context){
this.context = context.getApplicationContext();
}
在此之后我认为我得到了上下文然后我想要的是当分数大于 1 时应用程序应该关闭。
score = (i+1);
if (score >1)
{
}
我想在这种情况下关闭我的游戏,但它没有给我任何结束上下文的选项。请在我错的地方帮助我。我认为这个 Serializable class 中的上下文没有成功实现。需要帮助
仅供参考
在游戏 class 中关闭应用程序不是一个好方法,最好在 Activity 本身中进行。 一个很好的解决方案是在游戏 class 中创建一个方法,如下所示:
public boolean shouldCloseApplication(){
return this.score > 1;
}
并在 Activity 中使用以下内容:
if(game.shouldCloseApplication()){
//close the application as mentioned below
this.finishAffinity();
}
说,如果你想关闭应用程序,你只需要调用
this.finishAffinity();
refer to this SO post.
所以,不要传递“MainActivity
”,而是直接使用传递它的“Context
”
Game game = new Game(MainActivity.this);
并通过
接收public Game(Context context){
this.context = context;
}
并与
一起使用if(score > 1)
{
context.finishAffinity();
}
不建议,但有效
如果这不起作用,请使用
context.getActivity().finish();
System.exit(0);
希望对您有所帮助
试试这个
public Game(MainActivity context){
this.context = context;
}
score = (i+1);
if (score >1){
PackageManager pm = context.getPackageManager();
Intent i = new Intent("android.intent.action.MAIN");
i.addCategory("android.intent.category.HOME");
List<ResolveInfo> lst = pm.queryIntentActivities(i, 0);
if (lst != null) {
for (ResolveInfo resolveInfo : lst) {
try {
ApplicationInfo ai = pm.getApplicationInfo(
resolveInfo.activityInfo.packageName,0);
if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
Intent goHome = new Intent();
goHome.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
goHome.setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name);
context.startActivity(goHome);
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
break;
}
} catch (NameNotFoundException e) {
logMessage(LOG_TYPE_ERROR, "AppConstant-exitFromApp-kill", e.getMessage());
}
}
}
}