我想将传感器数据保存在文本文件中。但我的服务只能保存数据几分钟然后停止。我添加了WakeLock,但几分钟后它就没有将传感器数据保存到文件中。活动和服务类是 -
MainActivity.Class
public class MainActivity extends AppCompatActivity {
private MyService mBoundService;
boolean mIsBound;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((MyService.LocalBinder)service).getService();
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
},
10);
bindService(new Intent(this, MyService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
}
MyService.class
public class MyService extends Service implements SensorEventListener{
SensorManager mSensorManager;
Sensor mAccelerometer;
PowerManager.WakeLock cpuWakeLock;
public class LocalBinder extends Binder{
MyService getService(){
return MyService.this;
}
}
private final IBinder mBinder = new LocalBinder();
private void registerListener() {
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
private void unregisterListener() {
mSensorManager.unregisterListener(this);
}
public BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive("+intent+")");
if (!intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
return;
}
Runnable runnable = new Runnable() {
public void run() {
Log.i(TAG, "Runnable executing.");
unregisterListener();
registerListener();
}
};
new Handler().postDelayed(runnable, 500);
}
};
@Override
public void onCreate() {
super.onCreate();
mSensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
PowerManager pm= (PowerManager) getApplicationContext().getSystemService(getApplicationContext().POWER_SERVICE);
cpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
unregisterListener();
registerListener();
registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
registerListener();
cpuWakeLock.acquire();
return Service.START_STICKY;
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
String entryAcc = System.currentTimeMillis() + ", " + sensorEvent.values[0] + "," + sensorEvent.values[1] + ", " + sensorEvent.values[2] + "\n";
Log.e("String", "value" + entryAcc);
String dataPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyWear_Data/";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm");
Date now = new Date();
String fileName = "acc_" + formatter.format(now) + ".txt";
writeToPath(fileName, entryAcc, dataPath);
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
private String writeToPath(String fileName, String data, String path) {
FileOutputStream fos = null;
try {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
File myFile = new File(dir, fileName);
fos = new FileOutputStream(myFile, true);
fos.write(data.getBytes());
fos.close();
Log.e("WriteToFile", "filename : " + fileName);
return myFile.getPath();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
我还将wakelock添加到清单文件中。如何在需要时运行服务并将传感器数据保存到文件中?我正在构建支持android 4.2.2的Android手表应用程序。
答案 0 :(得分:1)
由于最近的变化,android会在一段时间后让每个服务进入睡眠状态,以便保留电池。确保服务运行的唯一方法是使其运行某种前景UI。将以下内容添加到您的服务中。
public static final int NOTIFICATION_ID = 1337;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
runAsForeground();
return START_NOT_STICKY;
}
private void runAsForeground() {
Intent notificationIntent = new Intent(this, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0,
notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setOngoing(true)
.setSmallIcon(R.drawable.your_icon_here)
.setContentTitle("Service title here")
.setColor(ContextCompat.getColor(this, R.color.white))
.setContentIntent(pendingIntent)
.build();
startForeground(NOTIFICATION_ID, notification);
}
通过添加通知,服务将具有UI,这将使其在OS前面具有更高的优先级。这意味着它只会在极少数情况下被杀死。