后台服务进入休眠状态
Background service going to sleep
我想将传感器数据保存在文本文件中。但是我的服务只能保存数据几分钟然后停止。我添加了 WakeLock,但几分钟后它没有将传感器数据保存到文件中。 activity 和服务 class 是-
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;
}
}
}
我也在清单文件中添加了唤醒锁。我如何 运行 服务直到我需要并将传感器数据保存到文件?我正在为支持 android 4.2.2.
的 android 手表构建应用程序
由于最近的更改 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 之前具有更高的优先级。这意味着它只会在极少数情况下被杀死。
我想将传感器数据保存在文本文件中。但是我的服务只能保存数据几分钟然后停止。我添加了 WakeLock,但几分钟后它没有将传感器数据保存到文件中。 activity 和服务 class 是-
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;
}
}
}
我也在清单文件中添加了唤醒锁。我如何 运行 服务直到我需要并将传感器数据保存到文件?我正在为支持 android 4.2.2.
的 android 手表构建应用程序由于最近的更改 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 之前具有更高的优先级。这意味着它只会在极少数情况下被杀死。