Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在进程终止后检查进程状态,或者这是否可能?_Java_Android_Linux_Object_Process - Fatal编程技术网

Java 如何在进程终止后检查进程状态,或者这是否可能?

Java 如何在进程终止后检查进程状态,或者这是否可能?,java,android,linux,object,process,Java,Android,Linux,Object,Process,某些代码故意引入空指针异常,如下所示: //==================================== // access modifiers omitted for brevity class WhatIsYourNameActivity extends Activity { void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); se

某些代码故意引入空指针异常,如下所示:

//====================================

// access modifiers omitted for brevity
class WhatIsYourNameActivity extends Activity {

    void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.writing);

        // Just assume that in the real app we would really ask it!
        MyApplication app = (MyApplication) getApplication();
        app.setName("Developer Phil");
        startActivity(new Intent(this, GreetLoudlyActivity.class));

    }

}
// access modifiers omitted for brevity
class GreetLoudlyActivity extends Activity {

    TextView textview;

    void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.reading);
        textview = (TextView) findViewById(R.id.message);
    }

    void onResume() {
        super.onResume();

        MyApplication app = (MyApplication) getApplication();
        textview.setText("HELLO " + app.getName().toUpperCase());
    }
}
//======================================================

// access modifiers omitted for brevity
class WhatIsYourNameActivity extends Activity {

    void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.writing);

        // Just assume that in the real app we would really ask it!
        MyApplication app = (MyApplication) getApplication();
        app.setName("Developer Phil");
        startActivity(new Intent(this, GreetLoudlyActivity.class));

    }

}
// access modifiers omitted for brevity
class GreetLoudlyActivity extends Activity {

    TextView textview;

    void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.reading);
        textview = (TextView) findViewById(R.id.message);
    }

    void onResume() {
        super.onResume();

        MyApplication app = (MyApplication) getApplication();
        textview.setText("HELLO " + app.getName().toUpperCase());
    }
}
用户启动应用程序。 在WhatIsYourNameActivity中,您询问用户的姓名,并将其存储在MyApplication中。 在GreetLoudlyActivity中,从MyApplication对象获取用户名并显示它。 用户使用“主页”按钮离开应用程序。 几个小时后,安卓悄悄地关闭了应用程序以回收一些内存

到目前为止,一切都很好

但这里有一个很粗糙的部分

用户重新打开应用程序。 Android创建一个新的MyApplication实例并恢复GreetLoudlyActivity。 GreetLoudlyActivity获取用户名,该用户名现在为空,并因NullPointerException而崩溃

崩溃发生的原因是应用程序对象是全新的,因此name变量为null,在调用字符串#toUpperCase()时导致NullPointerException

命令行,当您在模拟器或根手机上运行此命令时: 启动应用程序,然后:

adb shell ps 
在设备和命令行上按home键:

adb shell ps | grep com.crashy.package
以及:

adb shell kill

现在,我们尝试使用recents apps选项卡从后台恢复应用程序,它会按预期崩溃。问题是,我如何列出随进程一起终止的所有对象的状态,或者终止进程是否会终止所有关联对象?有没有一种方法可以完成这个过程?

问题是您试图用一个简单的变量来模拟持久性存储。为了更好地理解,我将研究Android活动的生命周期,看看应用程序如何以及何时被“杀死”

现在,我建议使用共享首选项管理器(您的数据看起来不够大,不足以证明使用sqlLite db是正确的)

在“存储活动”中,使用以下内容保存名称

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this)
Preferences.Editor edit = preferences.edit();
edit.putString('name', 'someName');
edit.commit();
并在您的其他活动中使用

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this)
preferences.getString('name');
“…或者终止进程是否会终止所有关联对象?”

事实就是这样。当一个进程被终止时,它拥有的所有内存都会被操作系统回收——如果没有发生这种情况,每次进程终止时都会出现内存泄漏,操作系统最终会耗尽内存


一旦进程/应用程序终止,您将丢失所有未保存到永久存储中的内容。

您可以将用户名存储在
WhatIsYourNameActivity
中,并将其作为
意图包传递到
GreetLoudlyActivity
并存储在那里。这不是我的问题。我有意介绍这个问题,我想分析当进程被终止时会发生什么。强引用的对象会发生什么情况?虽然这不是答案,但为什么要将名称存储在应用程序类中,为什么不存储在共享pref中?我正在创建一个测试环境,以了解进程是如何终止的,丢失了哪些变量,以及进程是如何重新创建的?这是一个纯粹的RnD问题,与现实生活中的问题无关。进程被终止后就不存在了。你的问题在措辞上存在矛盾,这不是我的问题。进程被终止时会发生什么?一个过程是分叉的?当一个进程被分叉时会发生什么?在分叉进程之后,子进程的地址空间将被新的进程数据覆盖。这是通过对系统的exec调用完成的。因此,fork和exec机制将旧命令切换为新命令,而执行新程序的环境保持不变,包括输入和输出设备、环境变量和优先级的配置。此机制用于创建所有UNIX进程,因此它也适用于Linux操作系统。即使是进程ID为1的第一个进程init,在所谓的引导过程中也会在引导过程中分叉?