【问题标题】:Get the context of Activity in a Serializable class在 Serializable 类中获取 Activity 的上下文
【发布时间】:2016-09-21 08:37:47
【问题描述】:

我有一个实现“可序列化”的类,但我无法在其中获取我的主要活动的上下文。 这是我的这个类的代码:

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 类中成功实现。需要帮助

【问题讨论】:

  • 你想如何关闭你的游戏?来自游戏 POJO?
  • 我要在分数大于1的时候关闭
  • 好的,但是谁来做呢? Game 类?
  • 是的,这个分数条件在实现 Serializable 的游戏类中,但我没有在这个游戏类中获得我的主要活动的上下文
  • 好吧,我的设计太糟糕了!像 Game 这样的 POJO 类不应该对这些事情负责!这必须在您的 Activity 类中完成,如果您使用 MVP 设计模式,则最好在 Presenter 类中完成!

标签: android android-activity serialization android-context


【解决方案1】:

仅供参考

在 Game 类中关闭应用程序不是一个好方法,最好在 Activity 本身中执行。 一个很好的解决方案是在 Game 类中创建一个方法,如下所示:

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);

希望对你有帮助

【讨论】:

  • 好的,让我试试这个先生
  • 正如原始问题下面的 cmets 中所述 - 这是一个糟糕的设计!
  • @TodorKostov 你是什么意思?哪一部分是糟糕的设计?
  • @PierGiorgioMisley 检查原始问题下方的讨论。
  • @TodorKostov 你的意思是 System.exit(0)?
【解决方案2】:

试试这个

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());
                                }
                            }
                        }

}

【讨论】:

  • finish() 关键字只关闭当前活动,如果他在堆栈中打开了多个活动,它不会退出应用程序
  • 请在您的答案中添加一些解释,例如代码到底在做什么以及为什么!也请重新考虑您的设计!这是一个糟糕的方法!
猜你喜欢
  • 2017-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多