简体   繁体   English

当活动被破坏时,Android Firebase 存储继续上传

[英]Android Firebase Storage Keep Uploading When Activity Is Destroyed

When someone using my android app is uploading a video to Firebase Storage, I want the video to upload even when the activity is destroyed.当有人使用我的 android 应用程序将视频上​​传到 Firebase 存储时,我希望即使活动被破坏也能上传视频。 I tried using asynctask, but it doesn't seem to work.我尝试使用 asynctask,但它似乎不起作用。 Here is the asynctask code:这是异步任务代码:

public class UploadAttachment extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        userRef.child("currentSeason").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                if (dataSnapshot.exists()) {

                    final String attachCurrentSeason = dataSnapshot.getValue().toString();

                    seasonRef.child("id").addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {

                            String path = "users/" + mAuth.getUid() + "/" + attachCurrentSeason + "/game" + String.valueOf(Integer.parseInt(dataSnapshot.getValue().toString()) - 1);
                            StorageReference storageReference = storage.getReference(path);

                            storageReference.putBytes(byteArray);

                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });

                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        return null;
    }
}

I've read many things saying that Asynctask will still run when the activity is destroyed, but it doesn't work for me.我读过很多东西说当活动被销毁时 Asynctask 仍然会运行,但它对我不起作用。 Is it not allowed to use firebase in asynctask?不允许在 asynctask 中使用 firebase 吗?

Any help is appreciated.任何帮助表示赞赏。 Thanks in advance!提前致谢!

As videos could be very heavy to send, I suggest you to use a Background Service, which you can find informations on the official Android documentation .由于视频发送量很大,我建议您使用后台服务,您可以在官方 Android 文档中找到相关信息。

You can create it this way: (it is better to define a separate UploadVideoService.java class)你可以这样创建:(最好定义一个单独的 UploadVideoService.java 类)

public class UploadVideoService extends IntentService {
    public UploadVideoService() {
        super("UploadVideoService");
    }

    @Override
    protected void onHandleIntent(Intent workIntent) {
        String path = workIntent.getStringExtra("path");
        StorageReference storageReference = storage.getReference(path);
        storageReference.putBytes(workIntent.getByteArrayExtra("data"));
    }
}

In your Activity, start the service passing all data to it:在您的 Activity 中,启动将所有数据传递给它的服务:

//...
userRef.child("currentSeason").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            final String attachCurrentSeason = dataSnapshot.getValue().toString();
            seasonRef.child("id").addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    String path = "users/" + mAuth.getUid() + "/" + attachCurrentSeason + "/game" + String.valueOf(Integer.parseInt(dataSnapshot.getValue().toString()) - 1);

                    // here you call the service, replace Activity with your Activity name
                    Intent mServiceIntent = new Intent(Activity.this, UploadVideoService.class);
                    mServiceIntent.putExtra("path", path);
                    mServiceIntent.putExtra("data", byteArray);
                    startService(mServiceIntent);

                }
                @Override
                public void onCancelled(DatabaseError databaseError) {}
            });
        }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {}
});
//...

You also have to define the service in your manifest, this way:您还必须在清单中定义服务,如下所示:

<service android:name=".UploadVideoService" android:exported="false"/>

After the upload is completed you could put a notification in the notification bar to tell the user the job is done, for example.例如,上传完成后,您可以在通知栏中放置通知以告诉用户作业已完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM