如何在它被杀死后检查进程状态,或者这甚至可能吗?
How to check the process status after it is killed, or is this even possible?
一些故意引入空指针异常的代码如下:
// access modifiers omitted for brevity
class MyApplication extends Application {
String name;
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
}
//====================================
// 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 默默地终止应用程序以回收一些内存。
到目前为止,一切顺利!
但是崩溃的部分来了……
用户重新打开应用。
Android 创建一个新的 MyApplication 实例并恢复 GreetLoudlyActivity。
GreetLoudlyActivity 获取用户名(现在为空)并因 NullPointerException 而崩溃。
崩溃的发生是因为 Application 对象是全新的,所以 name 变量为 null,当我们对其调用 String#toUpperCase() 时导致 NullPointerException。
命令行,当你在模拟器或 root phone 上 运行 时:
启动应用程序,然后:
adb shell ps
在设备和命令行上按主页:
adb shell ps | grep com.crashy.package
和:
adb shell kill <PID from above step>
现在,我们尝试使用最近使用的应用程序选项卡从后台恢复应用程序,但它按预期崩溃了。问题是我如何列出与进程一起被杀死的所有对象的状态 - 或者杀死进程会杀死所有关联的对象吗?有没有办法分叉这个过程?
问题是您正在尝试使用一个简单的变量来模拟持久存储。为了更好地理解,我会研究 Android Activity 的生命周期,以了解应用程序如何以及何时 "killed" http://developer.android.com/training/basics/activity-lifecycle/index.html
现在,对于一个解决方案,我建议使用共享首选项管理器。(您的数据看起来不够大,无法证明 sqlLite 数据库的合理性)
在你的存储中activity用这个保存名字
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this)
Preferences.Editor edit = preferences.edit();
edit.putString('name', 'someName');
edit.commit();
然后用
在你的另一个 activity 中检索它
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this)
preferences.getString('name');
"...or does killing a process kill all associated objects?"
事情就是这样。当一个进程被终止时,它拥有的所有内存都被 OS 回收——如果没有发生,每次进程终止时都会发生内存泄漏,并且 OS 最终会 运行 内存不足。
一旦 process/app 终止,您将丢失所有未保存到永久存储的内容。
一些故意引入空指针异常的代码如下:
// access modifiers omitted for brevity
class MyApplication extends Application {
String name;
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
}
//====================================
// 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 默默地终止应用程序以回收一些内存。
到目前为止,一切顺利!
但是崩溃的部分来了……
用户重新打开应用。 Android 创建一个新的 MyApplication 实例并恢复 GreetLoudlyActivity。 GreetLoudlyActivity 获取用户名(现在为空)并因 NullPointerException 而崩溃。
崩溃的发生是因为 Application 对象是全新的,所以 name 变量为 null,当我们对其调用 String#toUpperCase() 时导致 NullPointerException。
命令行,当你在模拟器或 root phone 上 运行 时: 启动应用程序,然后:
adb shell ps
在设备和命令行上按主页:
adb shell ps | grep com.crashy.package
和:
adb shell kill <PID from above step>
现在,我们尝试使用最近使用的应用程序选项卡从后台恢复应用程序,但它按预期崩溃了。问题是我如何列出与进程一起被杀死的所有对象的状态 - 或者杀死进程会杀死所有关联的对象吗?有没有办法分叉这个过程?
问题是您正在尝试使用一个简单的变量来模拟持久存储。为了更好地理解,我会研究 Android Activity 的生命周期,以了解应用程序如何以及何时 "killed" http://developer.android.com/training/basics/activity-lifecycle/index.html
现在,对于一个解决方案,我建议使用共享首选项管理器。(您的数据看起来不够大,无法证明 sqlLite 数据库的合理性)
在你的存储中activity用这个保存名字
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this)
Preferences.Editor edit = preferences.edit();
edit.putString('name', 'someName');
edit.commit();
然后用
在你的另一个 activity 中检索它SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this)
preferences.getString('name');
"...or does killing a process kill all associated objects?"
事情就是这样。当一个进程被终止时,它拥有的所有内存都被 OS 回收——如果没有发生,每次进程终止时都会发生内存泄漏,并且 OS 最终会 运行 内存不足。
一旦 process/app 终止,您将丢失所有未保存到永久存储的内容。