运行永不停止(或始终重新启动)的后台服务

时间:2017-11-30 12:45:45

标签: android wear-os

我正在构建一个Android Wear服务,它在后台运行并每三分钟记录一次设备数据。对于此日志记录,我使用Handler,它记录值,然后间隔为3分钟。这将用于监控一个月的用户行为,因此不应随意停止。

我在清单中也使用了android.intent.action.BOOT_COMPLETED,每当手表启动时,这都会成功运行应用程序。

我的问题是该服务可以正常运行12小时......然后突然停止。时间量似乎是不确定的,因为有时它会在40分钟后停止。我怀疑它被Android杀死了,但在这种情况下我希望它重新启动。我已将return START_STICKY包含在我的服务中,这样可以最大限度地减少被杀的可能性。

在某些解决方案中,我看到onDestroy()被覆盖,并且刷新广播被发送到服务。虽然这似乎有效,但每次onDestroy()完成一项工作时都会调用Handler函数。这导致服务每三分钟重新启动一次,而不是当应用程序被Android实际杀死时。

我还查看了意图操作,以确定是否可以定期调用我的服务。但是,除了android.intent.action.BOOT_COMPLETED之外,所有有用的都无法在Manifest文件中注册。由于该服务无法运行,registerReceiver()几乎没有用处。

有没有人知道一种技术要么a)保证服务不会被Android杀死;或者b)保证服务在被杀之后总是重新启动。任何帮助都会非常感激,因为我甚至开始考虑在设备上编写Cron作业并将其设置为每次执行我的应用程序三分钟!我也查看了AlarmManager课程,但我担心如果长时间跑步可能会被杀死。

请在下面找到我的代码:(AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.study">

    <uses-feature android:name="android.hardware.type.watch" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.DeviceDefault">

        <receiver android:name="com.example.study.MyServiceManager" android:enabled="true">
            <intent-filter>
                <action android:name="com.example.study.RestartAction" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

        <activity
            android:name="com.example.study.MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name="com.example.study.RecordService"
            android:exported="false" />
    </application>

</manifest>

RecordService.java

package com.example.study;

import android.Manifest;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.KeyguardManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;

import com.google.android.gms.location.LocationServices;
import android.os.Build;
import android.os.Handler;
import android.os.PowerManager;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;

import com.google.android.gms.location.*;
import com.google.android.gms.tasks.OnSuccessListener;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class RecordService extends IntentService {

    private static Context context;
    private final static int INTERVAL = 1000 * 5; // 1 minutes
    Handler handler = new Handler();
    Location myLocation;

    private FusedLocationProviderClient mFusedLocationClient;

    SensorManager sensorManager;
    SensorManager gyroSensorManager;
    SensorManager compassSensorManager;
    SensorEventListener sensorEventListener;
    float[] accValues;
    float[] gyroValues;
    float[] compassValues;

    Runnable handlerTask = new Runnable() {
        @Override
        public void run() {
            try {
                recordData();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            handler.postDelayed(handlerTask, INTERVAL);
        }
    };

    public RecordService() {
        super("RecordService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        handlerTask.run();
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    public void recordData() throws ExecutionException, InterruptedException {

        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(MainActivity.getAppContext());

        boolean fineLocationGranted = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
        boolean coarseLocationGranted = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;

        boolean isLocationEnabled = isLocationEnabled(this);

        if(fineLocationGranted && coarseLocationGranted && isLocationEnabled) {

            long startTime = System.currentTimeMillis();
            long timeWaiting = 0L;

            mFusedLocationClient.getLastLocation()
                    .addOnSuccessListener(MainActivity.getActivity(), new OnSuccessListener<Location>() {
                        @Override
                        public void onSuccess(Location location) {

                            if (location != null) {
                                myLocation = location;
                            } else {
                                System.out.print("");
                            }
                        }
                    });

            do {
                timeWaiting = System.currentTimeMillis() - startTime;
            } while (myLocation == null && timeWaiting < 200);

        }

        List<PackageInfo> packages = getPackageManager().getInstalledPackages(PackageManager.GET_PERMISSIONS);
        KeyguardManager kg = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        boolean isLocked = kg.isDeviceSecure();

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        gyroSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        compassSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorEventListener = new SensorEventListener() {

            @Override
            public void onSensorChanged(SensorEvent sensorEvent) {
                if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                    accValues = sensorEvent.values;
                } else if(sensorEvent.sensor.getType() == Sensor.TYPE_GYROSCOPE){
                    gyroValues = sensorEvent.values;
                } else if(sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
                    compassValues = sensorEvent.values;
                }
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int i) {}
        };

        try {
            sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
            gyroSensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_NORMAL);
            compassSensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
        } catch(SecurityException e) {
            e.printStackTrace();
        }

        long startTime2 = System.currentTimeMillis();
        long timeWaiting2 = 0L;

        do {
            timeWaiting2 = System.currentTimeMillis() - startTime2;
        } while((accValues == null || gyroValues == null || compassValues == null) && timeWaiting2 < 200);

        StringBuilder builder = new StringBuilder();
        builder.append("G" + (isLocationEnabled ? 1 : 0));
        builder.append(" L" + (isLocked ? 1 : 0));

        if(accValues != null && gyroValues != null && compassValues != null) {
            builder.append(" ACX" + accValues[0]);
            builder.append(" ACY" + accValues[1]);
            builder.append(" ACZ" + accValues[2]);
            builder.append(" GYX" + gyroValues[0]);
            builder.append(" GYY" + gyroValues[1]);
            builder.append(" GYZ" + gyroValues[2]);
            builder.append(" CMX" + gyroValues[0]);
            builder.append(" CMY" + gyroValues[1]);
            builder.append(" CMZ" + gyroValues[2]);
        }

        if(myLocation != null) {
            builder.append(" LT" + myLocation.getLatitude());
            builder.append(" LN" + myLocation.getLongitude());
        }
        builder.append("\n");

        for(PackageInfo info: packages) {
            int lastDot = info.packageName.lastIndexOf('.');
            builder.append(info.packageName + "\n");
            if(info.requestedPermissions != null) {
                for (String rqprm : info.requestedPermissions) {
                    int dot = rqprm.lastIndexOf('.');
                    builder.append(rqprm.substring(dot != -1 ? dot + 1 : 0) + " ");
                }
                builder.append("\n");

                for (int permflag : info.requestedPermissionsFlags) {
                    builder.append("" + permflag + " ");
                }
                builder.append("\n");
            }
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd--HH-mm-ss");
        String currentDateString = sdf.format(new Date());
        File log = new File("/sdcard/Recordings/" + currentDateString);

        try {
            if (!log.exists()) {
                log.createNewFile();
            }
            BufferedWriter writer = new BufferedWriter(new FileWriter(log, true));
            writer.append(builder.toString());
            writer.newLine();
            writer.close();

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

    }

    public static boolean isLocationEnabled(Context context) {
        int locationMode = 0;
        String locationProviders;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            try {
                locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);

            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
                return false;
            }

            return locationMode != Settings.Secure.LOCATION_MODE_OFF;

        } else {
            locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            return !TextUtils.isEmpty(locationProviders);
        }
    }

MainActivity.java

package com.example.study;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView mTextView;
    private static Context context;
    private static MainActivity activity;
    Intent serviceIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MainActivity.context = getApplicationContext();
        activity = this;

        serviceIntent = new Intent(this, RecordService.class);
        startForegroundService(serviceIntent)
        startService(serviceIntent);
    }

    public static Context getAppContext() {
        return MainActivity.context;
    }

    public static MainActivity getActivity() {
        return activity;
    }

}

MyServiceManager.java

package com.example.study;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class MyServiceManager extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

            Intent myIntent = new Intent(context, MainActivity.class);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(myIntent);
        }
    }

}

0 个答案:

没有答案