java - oreo 和 pie 中的后台服务问题

标签 java android background-service jobintentservice

基本上我想在后台发送纬度和经度它在奥利奥版本以下工作正常但我被困在那个部分。一切正常。我添加了一个后台 JobIntentService,它在 Oreo 和馅饼背景下工作正常,但是当我在 2 分钟后锁定屏幕时,它会禁用或停止向我的服务器发送位置。

public class LocationService extends JobIntentService implements LocationListener {

static final int JOB_ID = 1000; //Unique job ID.
RequestQueue requestQueue;
DBConnection con;
String Lat, Lng;
public static final int notify = 5000;  //interval between two services(Here Service run every 5 Minute)
private Handler mHandler = new Handler();   //run on another Thread to avoid crash
private Timer mTimer = null;
protected LocationManager locationManager;

@Override
public void onCreate() {
    super.onCreate();
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
   onGetLocation();
    if (mTimer != null) // Cancel if already existed
        mTimer.cancel();
    else
        mTimer = new Timer();   //recreate new
    mTimer.scheduleAtFixedRate(new LocationService.TimeDisplay(), 0, notify);   //Schedule task

}
public LocationService() {

}
public static void enqueueWork(Context context, Intent work) {
    enqueueWork(context, LocationService.class, JOB_ID, work);
}

@Override
protected void onHandleWork(@NonNull Intent intent) {
   UpdateLocation();

}
public void UpdateLocation()
{
    con = new DBConnection(LocationService.this);

    if(requestQueue==null) {
        requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue = Volley.newRequestQueue(getApplicationContext(), new StethoVolleyHurlStack());
    }
    StringRequest request = new StringRequest(Request.Method.POST,
            "https://example.com/api/" + "updatelocation.php", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("Response", response.toString() + " " + Lat.toString()+ " " + Lng.toString());
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("Response", error.toString());
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> map = new HashMap<String, String>();
            map.put("userid", con.getusename());
            map.put("lat", Lat);
            map.put("lng", Lng);

            return map;
        }
    };

    requestQueue.add(request);
}
public void onGetLocation() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&      ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

@Override
public void onLocationChanged(Location location) {

    Lng=String.valueOf(location.getLongitude());
    Lat=String.valueOf(location.getLatitude());
}

class TimeDisplay extends TimerTask {
    @Override
    public void run() {

        // run on another thread
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                UpdateLocation();
            }
        });
    }
}

最佳答案

是的,您需要在开始服务时添加通知

public class LocationService extends Service implements CallbackResponseListener, LocationListener {

public void onCreate() {
            super.onCreate();
        }
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            super.onStartCommand(intent, flags, startId);
            Log.e("Service Started", "onStartCommand!");
            createNotificationChannel();
            startTimer();
            return START_STICKY;
        }
     private void createNotificationChannel() {
            // Create the NotificationChannel, but only on API 26+ because
            // the NotificationChannel class is new and not in the support library
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CharSequence name = "Service channel";
                String description = "Service channel description";
                int importance = NotificationManager.IMPORTANCE_DEFAULT;
                NotificationChannel channel = new NotificationChannel("12", name, importance);
                channel.setDescription(description);
                // Register the channel with the system; you can't change the importance
                // or other notification behaviors after this
                NotificationManager notificationManager = getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
            }
            Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);

            PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0,
                    notificationIntent, 0);
            Notification notification = new NotificationCompat.Builder(this, "12")
                    .setContentTitle("Title")
                    .setContentText("App is running in background for location")
                    .setSmallIcon(R.drawable.notification_icon).setContentIntent(intent)
                    .build();
            startForeground(2001, notification);
        }
        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.e("Service EXIT", "ondestroy!");    
                Intent broadcastIntent = new Intent(getApplicationContext(), LocationBroadcastReceiver.class);
                sendBroadcast(broadcastIntent);

            stoptimertask();
        }


        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
     public void startTimer() {
            //set a new Timer
            try {
                timer = new Timer();   
                //initialize the TimerTask's job
                initializeTimerTask(); 
                //schedule the timer, to wake up every 1 second
                timer.schedule(timerTask, AppConstants.bgTaskStartDelay,2000); //
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 /**
     * it sets the timer to print the counter every x seconds
     */
    public void initializeTimerTask() {
        timerTask = new TimerTask() {
            public void run() {
                try {
                    // add your code here for schedule




                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    }
}

关于java - oreo 和 pie 中的后台服务问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58429941/

相关文章:

java - Spring JPA - 数据完整性关系

Android:更改进度对话框文本

安卓录像库

java - 如何获取文件名并显示在列表中

c# - 对 .Net core 3.1 后台服务进行单元测试时,无法解析 IConfiguration

android - 通过返回按钮关闭应用程序时后台服务停止

连接到 rfcomm0 的 Java rxtx 代码不起作用

java - 如何在运行 Junit 测试用例时创建用户 session

java - 图像和堆大小问题

android - 如何在 flutter 的后台运行时钟计时器?