如果我关闭我的app,firebase bug

时间:2017-08-29 04:13:10

标签: android firebase firebase-realtime-database firebase-storage

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    public static final String ANONYMOUS = "anonymous";
    public static final int RC_SIGN_IN = 1;
    private static final int RC_PHOTO_PICKER = 2;
    private String mUsername;

    // Firebase instance variables
    private FirebaseDatabase mFirebaseDatabase;
    private DatabaseReference mMessagesDatabaseReference;
    private ChildEventListener mChildEventListener;
    private FirebaseAuth mFirebaseAuth;
    private FirebaseAuth.AuthStateListener mAuthStateListener;
    private FirebaseStorage mFirebaseStorage;
    private StorageReference mChatPhotosStorageReference;

    private SeekBar seekBar;

    private RecyclerView recyclerView;
    private FloatingActionButton floatingActionButton;
    NotificationCompat.Builder notificationBuilder;
    VideoAdapter videoAdapter;
    List<Video> videoList;


    NotificationManager notificationManager;
    AlertDialog.Builder alertDialog;
    EditText input;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mUsername = ANONYMOUS;
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        floatingActionButton = (FloatingActionButton) findViewById(R.id.floatingactionbutton);
        videoList = new ArrayList();
        // Initialize Firebase components
        mFirebaseDatabase = FirebaseDatabase.getInstance();
        mFirebaseAuth = FirebaseAuth.getInstance();
        mFirebaseStorage = FirebaseStorage.getInstance();
        seekBar = (SeekBar) findViewById(R.id.seekbar);

        mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("videomessages");
        mChatPhotosStorageReference = mFirebaseStorage.getReference().child("videos");

        mAuthStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    // User is signed in
                    onSignedInInitialize(user.getDisplayName());
                } else {
                    // User is signed out
                    onSignedOutCleanup();
                    startActivityForResult(
                            AuthUI.getInstance()
                                    .createSignInIntentBuilder()
                                    .setIsSmartLockEnabled(false)
                                    .setProviders(
                                            AuthUI.EMAIL_PROVIDER,
                                            AuthUI.GOOGLE_PROVIDER)
                                    .build(),
                            RC_SIGN_IN);
                }
            }
        };





        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("video/*");
                intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
                startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);


            }
        });


        attachDatabaseReadListener();
 }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN) {
            if (resultCode == RESULT_OK) {
                // Sign-in succeeded, set up the UI
                Toast.makeText(this, "Signed in!", Toast.LENGTH_SHORT).show();
            } else if (resultCode == RESULT_CANCELED) {
                // Sign in was canceled by the user, finish the activity
                Toast.makeText(this, "Sign in canceled", Toast.LENGTH_SHORT).show();
                finish();
            }
        } else if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {
            final Uri selectedImageUri = data.getData();

            String uriString = selectedImageUri.toString();
            File myFile = new File(uriString);
            String path = myFile.getAbsolutePath();
            String displayName = null;

            if (uriString.startsWith("content://")) {
                Cursor cursor = null;
                try {
                    cursor = getApplicationContext().getContentResolver().query(selectedImageUri, null, null, null, null);
                    if (cursor != null && cursor.moveToFirst()) {
                        displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                    }
                } finally {
                    cursor.close();
                }
            } else if (uriString.startsWith("file://")) {
                displayName = myFile.getName();
            }

            alertDialog = new AlertDialog.Builder(MainActivity.this);
            alertDialog.setTitle("Upload");
            alertDialog.setMessage("Enter Name");

            input = new EditText(MainActivity.this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            input.setLayoutParams(lp);
            input.setText(displayName);
            alertDialog.setView(input);

            alertDialog.setPositiveButton("YES",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {


                            new MyAsyncTask().execute(selectedImageUri);

                        }});

            alertDialog.setNegativeButton("NO",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });

            alertDialog.show();



        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        mFirebaseAuth.addAuthStateListener(mAuthStateListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mAuthStateListener != null) {
            mFirebaseAuth.removeAuthStateListener(mAuthStateListener);
        }
    }

    private void onSignedInInitialize(String username) {
        mUsername = username;
        attachDatabaseReadListener();
    }

    private void onSignedOutCleanup() {
        mUsername = ANONYMOUS;

    }

    private void attachDatabaseReadListener() {

        mMessagesDatabaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                videoList.clear();
                for (DataSnapshot postSnapshot : snapshot.getChildren()) {

                    Video postSnapshotValue = postSnapshot.getValue(Video.class);
                    if (!videoList.contains(postSnapshotValue)) {
                        videoList.add(postSnapshotValue);
                        Log.i(TAG, "onDataChange: " + videoList);
                    }

                }

                videoAdapter = new VideoAdapter(videoList, MainActivity.this);
                recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));

                recyclerView.setAdapter(videoAdapter);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {


            }
        });
    }


    public class MyAsyncTask extends AsyncTask<Uri, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected Void doInBackground(final Uri... params) {
            final StorageReference photoRef = mChatPhotosStorageReference.child(params[0].getLastPathSegment());
            alertDialog.setView(input);

                photoRef.putFile(params[0])
                                    .addOnSuccessListener(MainActivity.this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                            // When the image has successfully uploaded, we get its download URL
                                            //  progressBar.setVisibility(View.VISIBLE);
                                            Uri downloadUrl = taskSnapshot.getDownloadUrl();
                                            //String nameUrl=taskSnapshot.getMetadata().getName();

                                            Video video = new Video(input.getText().toString().trim(),downloadUrl.toString());
                                            Log.i(TAG, "onSuccess: Video Uploaded");
                                            mMessagesDatabaseReference.push().setValue(video);
                                         //   mMessagesDatabaseReference.push().setValue(video.getVideoUrl());

                                        }
                                    }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                                @Override
                                public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                                    int  progress = (int) ((100 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount());

                                    seekBar.setProgress(progress);

                                    notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
                                            .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
                                            .setSmallIcon(R.mipmap.ic_launcher)
                                            .setContentText("Upload in progress")
                                            .setContentIntent(contentIntent(getApplicationContext()))
                                            .setAutoCancel(true);

                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                        notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
                                    }

                                    notificationManager = (NotificationManager)
                                            getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

                                    for (int incr = progress; incr <= 100; incr += 5) {


                                        notificationBuilder.setProgress(100, progress, false);

                                        notificationManager.notify(20, notificationBuilder.build());

                                    }
                                    if(progress>=100){
                                        notificationBuilder.setContentText("Upload complete").setProgress(0, 0, false);
                                        notificationManager.notify(20, notificationBuilder.build());

                                    }



                                }
                            });




           return null;
        }


    }

    private PendingIntent contentIntent(Context context) {

        Intent startActivityIntent = new Intent(context, MainActivity.class);
        return PendingIntent.getActivity(
                context,
                0,
                startActivityIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    }





}

我点击浮动操作按钮将视频上传到firebase存储。视频成功上传到存储。当视频上传时,我使用foll代码将视频名称及其下载URL推送到数据库

Video video = new Video(input.getText().toString().trim(),downloadUrl.toString());
                                            Log.i(TAG, "onSuccess: Video Uploaded");
                                            mMessagesDatabaseReference.push().setValue(video);

我想这是firebase中的错误。我不确定,但请听我说。 第一种情况: 当我将视频上传到firebase并保持我的应用程序处于打开状态直到视频上传时,视频名称和下载url将被推送到firebase数据库,视频会上传到存储空间。 第二种情况: 如果我将视频上传到firebase存储空间并打开其他应用程序(例如youtube)并在我的通知中,我会收到有关视频上传量和上传完整视频后的通知,无论我是否打开我的应用无论是否,firebase都不会将名称和视频网址推送到数据库,但会将视频上传到存储空间。 为什么会出现这种情况?有人可以指导我......

1 个答案:

答案 0 :(得分:0)

<强>更新

您正在photoRef.putFile(params[0])上使用活动范围的成功侦听器。 The documentation解释说:

  

在onStop()

期间将自动删除侦听器

当您的活动进入后台时,它将停止并移除侦听器。使用另一种不以Activity作为第一个参数的addOnSuccessListener()形式。

向嫌疑人setValue()添加完成侦听器。另外,记录input的值。我不确定活动停止时UI对象的状态会发生什么。

Video video = new Video(input.getText().toString().trim(),downloadUrl.toString());
Log.i(TAG, "onSuccess: Video Uploaded= " + input.getText());
mMessagesDatabaseReference.push().setValue(video, new DatabaseReference.CompletionListener() {
    @Override
    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
        if (databaseError == null) {
            Log.d(TAG, "onComplete: SUCCESS");
        } else {
            Log.e(TAG, "onComplete: FAILED ", databaseError.toException());
        }
    }
});

根据对评论的回应,听起来您的活动可能会在被放入后台时被销毁(而不是停止)。将此方法和日志语句添加到您的活动中以查看是否发生了这种情况:

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.i(TAG, "onDestroy: ");
}