我正在开发一个应用程序,通过某些方法将文件上传到 Firebase存储。为此,我使用后台服务以避免中断这可能会导致一些错误。
问题是我的服务类应该获得一些对象,例如
public StorageReference stoageRefer;
public Uri fileUri;
public UploadFinishedListener uploadFinishedListener;
要使用这些对象我创建了setter,它将帮助我修改它们。
public void setStoageRefer(StorageReference stoageRefer) {
this.stoageRefer = stoageRefer;
}
public void setFileUri(Uri fileUri) {
this.fileUri = fileUri;
}
public void setUploadFinishedListener(UploadFinishedListener uploadFinishedListener) {
this.uploadFinishedListener = uploadFinishedListener;
}
从我的活动类开始,我将启动此服务并将其设置在我的服务对象上:
UploaderService uploaderService = new UploaderService();
uploaderService.setFileUri(fileUri);
uploaderService.setStoageRefer(stoageRefer);
uploaderService.setUploadFinishedListener(new UploadFinishedListener() {
@Override
public void OnUploadFinished(Uri Uri) {
UserModel.setmImageUri(Uri.toString());
mDatabase.child("Users").child(firebaseAuth.getCurrentUser().getUid()).child("DailyStory").setValue(UserModel);
finish();
}
});
实际上,我尝试 赢得有用的方式。所有设置对象都是 null ;
startService(new Intent(this,uploaderService.getClass()));
我的整个服务类代码:UploaderService.class
public class UploaderService extends Service {
public StorageReference stoageRefer;
public Uri fileUri;
public UploadFinishedListener uploadFinishedListener;
Notification notification;
public void setStoageRefer(StorageReference stoageRefer) {
this.stoageRefer = stoageRefer;
}
public void setFileUri(Uri fileUri) {
this.fileUri = fileUri;
}
public void setUploadFinishedListener(UploadFinishedListener uploadFinishedListener) {
this.uploadFinishedListener = uploadFinishedListener;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
startToUpload();
}
public void NotificationProgress(int progress) {
Integer notificationID = 100;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext());
notificationBuilder.setOngoing(true)
.setContentTitle("File is uploading")
.setContentText("It may take a while...")
.setSmallIcon(R.mipmap.clagra_logo)
.setProgress(100, progress, false);
notification = notificationBuilder.build();
notificationManager.notify(notificationID, notification);
}
public void cancelNotification(Context ctx, int notifyId) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancel(notifyId);
}
public void startToUpload() {
UploadTask uploadTask = stoageRefer.putFile(Uri.fromFile(new File(fileUri.getPath())));
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.d("uploadFail", "" + exception);
Toast.makeText(UploaderService.this, "Something went wrong", Toast.LENGTH_SHORT).show();
stopSelf();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
uploadFinishedListener.OnUploadFinished(downloadUrl);
cancelNotification(getApplicationContext(), 100);
stopSelf();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
NotificationProgress((int) progress);
Log.v("TAgTAG", progress + "");
}
});
}
public final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
public UploaderService getService() {
return UploaderService.this;
}
}
}
现在我想知道有没有办法从它的对象启动Start或Service。 提前谢谢......