Java 如果连接稳定,使用Android WorkManager运行函数
我目前有一个安卓应用程序,用户在其中拍摄一个项目的图片,然后用一些信息覆盖并保存为位图。 然后我有一个功能,可以使用电子邮件将此图像发送到已设置的共享邮箱 我的公司遇到的一个问题是,有时会出现WI-FI中断的黑点,因此应用程序无法发送图像 有人建议我使用Android WorkManager来安排同步尚未发送的图像 问题是我不能只将位图发送到Worker类,因为我需要运行完整的“sendfile()”函数,以便它作为电子邮件发送 以下是我正在尝试的一些代码(改编自教程): 以下是我尝试过的一些代码: MyWorker-工人类:Java 如果连接稳定,使用Android WorkManager运行函数,java,android,android-workmanager,Java,Android,Android Workmanager,我目前有一个安卓应用程序,用户在其中拍摄一个项目的图片,然后用一些信息覆盖并保存为位图。 然后我有一个功能,可以使用电子邮件将此图像发送到已设置的共享邮箱 我的公司遇到的一个问题是,有时会出现WI-FI中断的黑点,因此应用程序无法发送图像 有人建议我使用Android WorkManager来安排同步尚未发送的图像 问题是我不能只将位图发送到Worker类,因为我需要运行完整的“sendfile()”函数,以便它作为电子邮件发送 以下是我正在尝试的一些代码(改编自教程): 以下是我尝试过的一些代
public class MyWorker extends Worker {
//a public static string that will be used as the key
//for sending and receiving data
public static final String TASK_DESC = "task_desc";
public MyWorker(@NonNull Context context, @NonNull
WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
Camera1 camera1 = new Camera1();
camera1.sendPortraitImage();
// //getting the input data
// String taskDesc =
getInputData().getString(TASK_DESC);
//
// displayNotification("My Worker",
taskDesc);
// return Result.SUCCESS;
}
private void displayNotification(String title, String task) {
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("simplifiedcoding", "simplifiedcoding", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), "simplifiedcoding")
.setContentTitle(title)
.setContentText(task)
.setSmallIcon(R.mipmap.ic_launcher);
notificationManager.notify(1, notification.build());
}
}
WorkloadMangerSetUp()和SendFile()函数::
public void WorkManagerSetUp() {
//WORKMANAGERCODE
//creating a data object
//to pass the data with workRequest
//we can put as many variables needed
Data data = new Data.Builder()
.putString(MyWorker.TASK_DESC, fileLoc)
.build();
//creating constraints
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED) // you can add as many constraints as you want
.build();
final OneTimeWorkRequest workRequest =
new OneTimeWorkRequest.Builder(MyWorker.class)
.setInputData(data)
.setConstraints(constraints)
.build();
}
public void sendPortraitImage(View vw){
send.setVisibility(View.GONE);
buttonCamera.setVisibility(View.GONE);
bitmap_view = ScreenShott.getInstance().takeScreenShotOfRootView(vw, null);
//TextViewTypeHeader.setVisibility(View.GONE);
//TextViewTypeNo.setVisibility(View.GONE);
//TextViewImageHeader.setVisibility(View.GONE);
//TextViewImageNo.setVisibility(View.GONE);
saveImageToExternalStorage(bitmap_view);
Toast.makeText(getApplicationContext(), "Saved successfully, Check gallery", Toast.LENGTH_SHORT).show();
email = email_auth.EMAIL_FROM;
sub = TypeHeader + " - " + TypeNo + " - " + ImageNo;
msg = "Test AlmaPix";
fileLoc = file.getAbsolutePath();
SharedPreferences info = this.getSharedPreferences("info", MODE_PRIVATE);
info.edit().putString("info", fileLoc).commit();
mail mail = new mail(this,email ,sub, msg);
mail.execute();
WorkManagerSetUp();
WorkManager.getInstance().enqueue(workRequest);
}
我试图实现的是,我能够在MyWorker类中运行sendFile()函数,以便在WI-FI连接再次激活时发送所有未发送的电子邮件
任何帮助或建议我都会非常感激