【问题标题】:Take Photo In Background Service Not Working If Apps closed (Android Studio)如果应用程序关闭,后台拍照服务不工作(Android Studio)
【发布时间】:2020-02-07 03:47:34
【问题描述】:

我申请了后台服务拍照。在应用程序打开时工作,但在应用程序关闭时不工作。我用设置间隔的警报管理器做到了。

 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】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多