gpt4 book ai didi

android - 如何在android中创建一个继续运行的后台文件上传?

转载 作者:搜寻专家 更新时间:2023-11-01 07:44:03 26 4
gpt4 key购买 nike

我正在开发一个 android 应用程序,它可以在不中断 UI 的情况下从后台将特定文件夹中的图像一张一张地上传到服务器。我实现的是从具有运行时权限的启动器 Activity 调用 intentService 来读取 READ_EXTERNAL_STORAGE。我需要上传文件夹中的所有文件,还需要上传创建或移动到文件夹的新文件。为此,我创建了一个文件观察器类,我认为它无法正常工作。

  public class DirectoryFileObserver extends FileObserver {
String aboslutePath = "";

public DirectoryFileObserver(String path) {
super(path, FileObserver.ALL_EVENTS);
aboslutePath = path;
Log.i("watch path", path);
}

@Override
public void onEvent(int event, String path) {
Log.i("FileObserver++", "File Created:" + path);
File file = new File(aboslutePath + "/" + path);
List<Skeem> mSkeemList = Skeem.find(Skeem.class, "file = ?", new String[]{file.getAbsolutePath()});
if (mSkeemList.size() == 0) {
Skeem mSkeem = new Skeem();
mSkeem.setUsername(AppConstants.UserEmail);
mSkeem.setFolderName(aboslutePath);
mSkeem.setFile(file.getAbsolutePath());
uploadService uploadService = new uploadService();
uploadService.upload(mSkeem);

}

switch (event) {
case FileObserver.ALL_EVENTS:
Log.d("All", "Path" + path);
break;
case FileObserver.CREATE:
Log.d("Create", "Path" + path);
break;
}


}
}

还创建了一个如下的广播接收器,以在保留网络连接时恢复文件上传

public class InternetConnector_Receiver extends BroadcastReceiver {
public InternetConnector_Receiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
try {

boolean isVisible = BaseApplication
.isActivityVisible();// Check if
// activity
// is
// visible
// or not
Log.i("Activity is Visible ", "Is activity visible : " + isVisible);

// If it is visible then trigger the task else do nothing
if (isVisible == true) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager
.getActiveNetworkInfo();

// Check internet connection and accrding to state change the
// text of activity by calling method
if (networkInfo != null && networkInfo.isConnected()) {

Intent serviceIntent = new Intent(context, uploadService.class);
context.startService(serviceIntent);

} else {

}
}
} catch (Exception e) {
e.printStackTrace();
}

}
}

并在 list 中

   <receiver
android:name=".InternetConnector_Receiver"
android:enabled="true">
<intent-filter>
<!-- Intent filters for broadcast receiver -->
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>

我将所有文件添加到数据库,状态为 false。

    File[] files = directory.listFiles();
for (int n = 0; n < skeemList.size(); n++) {
Skeem skeem = skeemList.get(n);
for (int o = 0; o < files.length; o++) {
if (!(skeem.getFile().equalsIgnoreCase(files[o].getAbsolutePath()))) {
if (!files[o].isDirectory()) {
Skeem mSkeem = new Skeem();
mSkeem.setFile(files[o].getAbsolutePath());
mSkeem.setFolderName(directory.getName());
mSkeem.setUsername(email);
upload(mSkeem);
}
}
}
}

然后我开始上传状态为 false 的文件并更新状态为 true 的表。当我启动应用程序时,图像上传成功开始。但一段时间后它停止了。我是否使用正确的服务从后台上传文件?有什么办法可以上传文件夹的内容吗?我浏览了很多网站和链接。但是我找不到我需要的确切解决方案。请帮我。

最佳答案

有了新的Background Limitations对于服务执行,您需要对后台服务非常谨慎。我建议您使用 JobIntentService 而不是 IntentService正如谷歌建议的那样。 Pre-Oreo 它充当 IntentService,在(Post-)Oreo 上它使用 Jobs 做后台工作。记得请求WAKE_LOCK权限

关于android - 如何在android中创建一个继续运行的后台文件上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48549284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com