新 activity 有时无法启动
New activity sometimes doesn't start
在某些情况下,我需要在我的应用程序中调用 exit()
(我知道这不是最好的完成方式,但这不是问题所在)。我还想显示一个新对话框,通知用户有关崩溃的信息。
我创建了一个新的 activity class,一个新的广播接收器,并在清单中注册了它们。接下来,我调用:
Intent intent = new Intent(this, AppCloseReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
try
{
pendingIntent.send();
}
catch(Exception ex){}
System.exit(0);
问题是 有时 会出现新的 window!首先我认为 System.exit(0);
在新的 activity 有机会开始之前触发(因为异步调用,我认为在文档中找不到这个)所以我在 [=15= 之间添加了 Thread.sleep(1000)
] 和 System.exit(0);
,但结果是一样的 - 新的 window 出现 有时 。日志中没有任何内容,无一例外。
新的 activity 只是一个静态文本。
这不可靠。如果您导致 VM 关闭,您将无法显示任何内容,因为不再有 VM 运行ning。唯一可靠的方法是确保 BroadcastReceiver
和 Activity
运行 在不同的 OS 进程中显示您的消息。这也不是 100% 可靠的,因为根据异常的性质,您现有的 VM 可能无法启动其他组件,但它可能比您当前的实施更可靠。例如,如果您的应用程序因 OutOfMemoryException
而崩溃,则可能无法执行任何有用的操作。
要确保组件 运行 在单独的进程中,请添加
android:process=":other"
这些组件的清单中的 <activity>
和 <receiver>
定义。
您还应该尝试延迟对 System.exit()
的调用,让 VM 有机会实际启动对话。此外,您不需要为此使用 PendingIntent
。尝试这样的事情:
Intent intent = new Intent(this, AppCloseReceiver.class);
sendBrodcast(intent);
// Start separate Thread to terminate the process
new Thread(new Runnable() {
@override
public void run() {
SystemClock.sleep(1000); // Sleep a bit to give the VM enough time to actually send the broadcast Intent
System.exit(0);
}
}).start();
在某些情况下,我需要在我的应用程序中调用 exit()
(我知道这不是最好的完成方式,但这不是问题所在)。我还想显示一个新对话框,通知用户有关崩溃的信息。
我创建了一个新的 activity class,一个新的广播接收器,并在清单中注册了它们。接下来,我调用:
Intent intent = new Intent(this, AppCloseReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
try
{
pendingIntent.send();
}
catch(Exception ex){}
System.exit(0);
问题是 有时 会出现新的 window!首先我认为 System.exit(0);
在新的 activity 有机会开始之前触发(因为异步调用,我认为在文档中找不到这个)所以我在 [=15= 之间添加了 Thread.sleep(1000)
] 和 System.exit(0);
,但结果是一样的 - 新的 window 出现 有时 。日志中没有任何内容,无一例外。
新的 activity 只是一个静态文本。
这不可靠。如果您导致 VM 关闭,您将无法显示任何内容,因为不再有 VM 运行ning。唯一可靠的方法是确保 BroadcastReceiver
和 Activity
运行 在不同的 OS 进程中显示您的消息。这也不是 100% 可靠的,因为根据异常的性质,您现有的 VM 可能无法启动其他组件,但它可能比您当前的实施更可靠。例如,如果您的应用程序因 OutOfMemoryException
而崩溃,则可能无法执行任何有用的操作。
要确保组件 运行 在单独的进程中,请添加
android:process=":other"
这些组件的清单中的 <activity>
和 <receiver>
定义。
您还应该尝试延迟对 System.exit()
的调用,让 VM 有机会实际启动对话。此外,您不需要为此使用 PendingIntent
。尝试这样的事情:
Intent intent = new Intent(this, AppCloseReceiver.class);
sendBrodcast(intent);
// Start separate Thread to terminate the process
new Thread(new Runnable() {
@override
public void run() {
SystemClock.sleep(1000); // Sleep a bit to give the VM enough time to actually send the broadcast Intent
System.exit(0);
}
}).start();