如果应用程序关闭,在后台服务中拍照将无法使用(Android Studio)

问题描述 投票:0回答:1

我提出了一项在后台服务中拍照的应用程序。在应用程序打开时有效,但在应用程序关闭时无效。我是通过警报管理器按设定的时间间隔完成的。

 public void onReceive(final Context context, Intent intent) {
    // get location
    FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context);
    Task<Location> task = fusedLocationProviderClient.getLastLocation();
    task.addOnSuccessListener(new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
            if (location != null){
                currentLocation = location;
                String pref_nip = SharedPrefManager.getInstance(context).getNip();
                push_loc(context,pref_nip,String.valueOf(currentLocation.getLongitude()),String.valueOf(currentLocation.getLatitude()));
            }else{
                Toast.makeText(context,"yah gagal",Toast.LENGTH_SHORT).show();
            }
        }
    });
    task.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
             Toast.makeText(context,"Error map",Toast.LENGTH_LONG).show();
        }
    });
    openCamera();
    // insert code here
}

private void openCamera() {
    CaptureImage captureImage = new CaptureImage();
    captureImage.getImage();
}

public void startRepeatingBroadcast()
{
    ComponentName receiver = new ComponentName(this, MyReceiver.class);
    PackageManager pm = this.getPackageManager();

    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);

    AlarmManager am=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, MyReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);
    am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),1000 * 60 * 2,pi);
}

获取位置时的工作。

java android android-studio alarmmanager
1个回答
0
投票

自android 8.0更新以来,这是不可能的,您不能在不让用户知道您正在执行此操作的情况下在后台使用这种服务(通知栏中有些通知之王)您可能会发现此链接有用:Create your own accessibility service

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.