[英]Android - Resuming application state - SL4A
请不要因为一个noob-ish问题而责备我。
我正在使用SL4A开发一个Android应用程序,当我的应用程序启动时,它会在脚本执行时在后台运行。 我不知道从哪里开始,但每次点击我的图标,它都会重新启动我的应用程序。 我尝试使用不同的启动模式,没有任何不同的发生。 我认为它与OnCreate代码和通知设置有关。 我需要帮助保存我的应用程序状态,然后重新单击图标或从通知栏单击继续。 我已经尝试过所有必须转向这里寻求帮助。 无论如何我都不是Android编程的专业人士。 谢谢你们,温柔;)
Public void onCreate() {
super.onCreate();
mInterpreterConfiguration = ((BaseApplication) getApplication())
.getInterpreterConfiguration();
}
@Override
public void onStart(Intent intent, final int startId) {
super.onStart(intent, startId);
String fileName = Script.getFileName(this);
Interpreter interpreter = mInterpreterConfiguration
.getInterpreterForScript(fileName);
if (interpreter == null || !interpreter.isInstalled()) {
mLatch.countDown();
if (FeaturedInterpreters.isSupported(fileName)) {
Intent i = new Intent(this, DialogActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(Constants.EXTRA_SCRIPT_PATH, fileName);
startActivity(i);
} else {
Log
.e(this, "Cannot find an interpreter for script "
+ fileName);
}
stopSelf(startId);
return;
}
// Copies script to internal memory.
fileName = InterpreterUtils.getInterpreterRoot(this).getAbsolutePath()
+ "/" + fileName;
File script = new File(fileName);
// TODO(raaar): Check size here!
if (!script.exists()) {
script = FileUtils.copyFromStream(fileName, getResources()
.openRawResource(Script.ID));
}
copyResourcesToLocal(); // Copy all resources
if (Script.getFileExtension(this)
.equals(HtmlInterpreter.HTML_EXTENSION)) {
HtmlActivityTask htmlTask = ScriptLauncher.launchHtmlScript(script,
this, intent, mInterpreterConfiguration);
mFacadeManager = htmlTask.getRpcReceiverManager();
mLatch.countDown();
stopSelf(startId);
} else {
mProxy = new AndroidProxy(this, null, true);
mProxy.startLocal();
mLatch.countDown();
ScriptLauncher.launchScript(script, mInterpreterConfiguration,
mProxy, new Runnable() {
@Override
public void run() {
mProxy.shutdown();
stopSelf(startId);
}
});
}
}
RpcReceiverManager getRpcReceiverManager() throws InterruptedException {
mLatch.await();
if (mFacadeManager==null) { // Facade manage may not be available on startup.
mFacadeManager = mProxy.getRpcReceiverManagerFactory()
.getRpcReceiverManagers().get(0);
}
return mFacadeManager;
}
@Override
protected Notification createNotification() {
Notification notification =
new Notification(R.drawable.script_logo_48, this.getString(R.string.loading), System.currentTimeMillis());
// This contentIntent is a noop.
PendingIntent contentIntent = PendingIntent.getService(this, 0, new Intent(), 0);
notification.setLatestEventInfo(this, this.getString(R.string.app_name), this.getString(R.string.loading), contentIntent);
notification.flags = Notification.FLAG_ONGOING_EVENT;
return notification;
}
你在这里有几个不同的问题。 首先,在应用程序中执行某些工作的脚本可能不应该在Activity内部启动。 这是因为您可能不希望脚本的执行与Acitivty的生命周期相关联。 我建议查看活动生命周期@ http://developer.android.com/reference/android/app/Activity.html
我建议使用服务来控制脚本的工作。 正如文档所述
服务是一种应用程序组件,表示应用程序希望在不与用户交互的情况下执行较长时间运行的操作,或者为其他应用程序提供使用的功能。
该服务通常也是您希望控制通知显示和取消的位置。 就通知意图的活动启动模式而言,这可能会变得复杂,但通常您需要确保从通知中启动活动不会导致创建重复活动。 看看@ http://developer.android.com/guide/topics/ui/notifiers/notifications.html上的一些细节。
onRestart将触发onStart,因此每次重新启动应用程序时,都会再次触发脚本。 查看http://www.androidjavadoc.com/1.0_r1_src/android/app/Activity.html以获得方便的Android生命周期流程。
我想知道这是否会更好地作为服务实现,活动只是触发服务?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.