Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.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
Android数据存储性能_Android_Mobile - Fatal编程技术网

Android数据存储性能

Android数据存储性能,android,mobile,Android,Mobile,实际上,我正在开发一个带有数据存储的Android应用程序,我是这样进行的: 活动->业务服务->回购(带Spring REST fw)。使用此方法,我被迫让活动在关闭它之前完成其存储作业(线程处理、进度对话框…) 使用android服务存储数据是不是一种糟糕的编码方式 有了它,用户可以继续导航,并给人以使用非常流畅的应用程序的印象。这是一个好的解决方案吗 谢谢无需让您的活动在前台等待后台逻辑完成 相反,您应该以一种与您的活动“分离”的方式执行此后台逻辑 解决这个问题有两种方法:风险和安全 冒

实际上,我正在开发一个带有数据存储的Android应用程序,我是这样进行的:

活动->业务服务->回购(带Spring REST fw)。使用此方法,我被迫让活动在关闭它之前完成其存储作业(线程处理、进度对话框…)

使用android服务存储数据是不是一种糟糕的编码方式

有了它,用户可以继续导航,并给人以使用非常流畅的应用程序的印象。这是一个好的解决方案吗


谢谢

无需让您的活动在前台等待后台逻辑完成

相反,您应该以一种与您的活动“分离”的方式执行此后台逻辑

解决这个问题有两种方法:风险和安全


冒险之路

您可以使用AsyncTask或任何java.concurrent构造来代替线程。他们都会做这项工作

我已经用这种方法很多年了。它大部分工作正常。但是它本身就有缺陷
为什么?因为一旦活动完成()-ed,Android可以随时回收它及其所有资源,包括停止所有工作线程
如果您的长时间运行工作不超过几秒钟,并且我假设您的回购更新是这样的,那么这里的风险 这是最小的。但是为什么要接受它呢


安全之路

声明一个服务,并在活动停止之前激活它以执行长时间运行的操作:

class MyActivity extends Activity {

     void calledWhenActivityNeedsToBeClosed() {

          // delegate long running work to service
          startService(this, new Intent(this, MyWorkerService.class));

          // and close the activity without waiting for the thread to complete
          this.finish();
     }

}

这要安全得多。Android也可以,而且经常会,杀死正在运行的服务,但它比杀死后台活动更不情愿


请注意,如果您可以看到这样一个场景,即当辅助服务仍在运行时,您的UI是可见的, 您可能希望使用一个


最后,如果你想绝对确信Android不会清除后台逻辑,你可以 应该使用一个。下面是如何做到这一点的,但请注意——在您所描述的情况下,前台服务可能过于工程化了:



谢谢你的回答。还有一个每次都在后台运行的服务呢:他的目标是存储我们连接网络时没有保存的数据。即使在我们有网络连接的情况下,使用它来存储数据是一种糟糕的方式吗?在需要时将服务绑定到活动?
class MyActivity extends Activity {

     void calledWhenActivityNeedsToBeClosed() {

          // delegate long running work to service
          startService(this, new Intent(this, MyWorkerService.class));

          // and close the activity without waiting for the thread to complete
          this.finish();
     }

}
static final int NOTIF_ID = 100;

// Create the FG service intent 
Intent intent = new Intent(getApplicationContext(), MyActivity.class); // set notification activity
showTaskIntent.setAction(Intent.ACTION_MAIN);
showTaskIntent.addCategory(Intent.CATEGORY_LAUNCHER);
showTaskIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent pIntent = PendingIntent.getActivity(
                getApplicationContext(),
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

Notification notif = new Notification.Builder(getApplicationContext())
                .setContentTitle(getString(R.string.app_name))
                .setContentText(contentText)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentIntent(pIntent)
                .build();

startForeground(NOTIF_ID, notif);