我有一个必须在固定进程中运行的后台任务。我目前正在JobIntentService
为此使用 a 。但是,我想迁移到 WorkManager 以利用基于网络的任务延迟。
使用 WorkManager 或任何其他 API 14-28 兼容方式是否有任何解决方案?
我还是新手.我使用stopService调用onDestroy方法.调用onDestroy方法,因为我可以看到toast消息.但是用于发送位置更新的无限for循环一直在运行.有什么我需要改变的吗?
public class myservice extends IntentService {
LocationManager locationManager ;
public myservice() {
super(myservice.class.getName());
}
@Override
protected void onHandleIntent(Intent intent) {
String numb=intent.getStringExtra("num");
for (;;) {
send(numb);
}
}
public IBinder onBind(Intent intent) {
return null;
}
public void send(String numb) {
locationManager = (LocationManager)this
.getSystemService(Context.LOCATION_SERVICE);
if (ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_COARSE_LOCATION ) == PackageManager.PERMISSION_GRANTED ) {}
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Geocoder geocoder;
geocoder = new Geocoder(this, Locale.getDefault());
try {
Thread.sleep(3000);
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
Toast toast1 = Toast.makeText(this, "message …
Run Code Online (Sandbox Code Playgroud) 我实际上能够捕获照片并将其保存在 android 外部存储 DCIM 文件夹中。
我的问题是我无法在其中创建新文件夹,因此DCIM/MyPic.jpg变为DCIM/MyFolder/MyPic.jpg。
这是我的源代码:
File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "address");
if (!f.exists()) {
f.mkdir();
}
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator
+ "DCIM"
+ File.separator
+ "address"
+ File.separator
, "IMG_001.jpg");
Run Code Online (Sandbox Code Playgroud)
请注意,我在清单中正确地要求了 WRITE_EXTERNAL_STORAGE 权限。
捕获照片的意图部分很好,因为我可以将其直接保存到 DCIM。
我没有收到任何错误消息,但没有任何反应......没有创建“地址”文件夹:(
感谢您的帮助:D