更新:我还没有找到问题的真正解决方案.我所做的是一种在连接丢失时自动重新连接到以前的蓝牙设备的方法.它并不理想,但似乎运作得相当好.我很想听到有关这个的更多建议.
我遇到的问题与此问题大致相同:服务在被唤醒时被杀死并且在调用startForeground之后包括设备(Asus Transformer),服务停止前的时间长度(30-45分钟),使用唤醒锁定,使用startForeground(),以及当屏幕关闭时应用程序打开时不会发生问题的事实.
我的应用程序维护与另一台设备的蓝牙连接,并在两者之间发送数据,因此它必须始终处于活动状态以侦听数据.用户可以随意启动和停止服务,实际上这是我实现启动或停止服务的唯一方法.服务重新启动后,与其他设备的蓝牙连接将丢失.
根据链接问题中的答案,startForeground()"降低了服务被杀的可能性,但并未阻止它".我理解是这样的,但是我已经看到很多其他应用程序没有这个问题的例子(例如,Tasker).
如果服务能够在用户停止之前运行,我的应用程序的实用性将大大降低.有什么办法可以避免这个???
每当服务停止时,我都会在logcat中看到这个:
ActivityManager: No longer want com.howettl.textab (pid 32321): hidden #16
WindowManager: WIN DEATH: Window{40e2d968 com.howettl.textab/com.howettl.textab.TexTab paused=false
ActivityManager: Scheduling restart of crashed service com.howettl.textab/.TexTabService in 5000ms
Run Code Online (Sandbox Code Playgroud)
编辑:我还应该注意,这似乎没有出现在我连接的其他设备上:HTC Legend运行Cyanogen
编辑:这是输出adb shell dumpsys activity services:
* ServiceRecord{40f632e8 com.howettl.textab/.TexTabService}
intent={cmp=com.howettl.textab/.TexTabService}
packageName=com.howettl.textab
processName=com.howettl.textab
baseDir=/data/app/com.howettl.textab-1.apk
resDir=/data/app/com.howettl.textab-1.apk
dataDir=/data/data/com.howettl.textab
app=ProcessRecord{40bb0098 2995:com.howettl.textab/10104}
isForeground=true foregroundId=2 foregroundNoti=Notification(contentView=com.howettl.textab/0x1090087 vibrate=null,sound=null,defaults=0x0,flags=0x6a)
createTime=-25m42s123ms lastActivity=-25m42s27ms
executingStart=-25m42s27ms restartTime=-25m42s124ms
startRequested=true stopIfKilled=false callStart=true lastStartId=1
Bindings:
* IntentBindRecord{40a02618}:
intent={cmp=com.howettl.textab/.TexTabService}
binder=android.os.BinderProxy@40a9ff70
requested=true received=true hasBound=true doRebind=false
* Client AppBindRecord{40a3b780 ProcessRecord{40bb0098 …Run Code Online (Sandbox Code Playgroud) 我在应用程序中使用服务来监听用户按下他/她的电源按钮的次数.实施在所有设备上都运行良好.但是当我在Android Kitkat上测试应用程序时,我注意到了一些错误.
只要我将应用程序从最近的应用程序中移开,应用程序就不再会监听电源按钮.
这是我正在使用的代码:
public class Receiver extends Service {
Notification notification;
private static final int NOTIFICATION_ID = 0;
NotificationManager manager;
PendingIntent toOpen;
Intent intent;
private BroadcastReceiver POWER_BUTTON = new Powerbuttonrecceiver();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(POWER_BUTTON, filter);
startNotify();
return START_STICKY;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
unregisterReceiver(POWER_BUTTON);
dismissNotification();
super.onDestroy(); …Run Code Online (Sandbox Code Playgroud) 我已经startforeground()在我用了Service,但是Service当我清除所有最近的应用程序时,Android一直在杀死我.
这是我的服务代码:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Text")
.setTicker("Text")
.setContentText("Text")
.setSmallIcon(R.drawable.icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
.build();
startForeground(100, notification);
return START_STICKY;
}
Run Code Online (Sandbox Code Playgroud)
我用这段代码做错了吗?
我在我的代码中设置了警报,以便在特定时间内启动.
警报机制在SDK <19时运行良好,但在19日不会触发警报.
这是我设置闹钟的代码:
public void SetAlarm(Context context, Long executionTime)
{
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReciever.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Only one alarm can live, so cancel previous.
am.cancel(pi);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
am.set(AlarmManager.RTC_WAKEUP, executionTime, pi);
} else {
setAlarmFromKitkat(am, executionTime, pi);
}
}
Run Code Online (Sandbox Code Playgroud)
因为我使用Service我GetApplicationContext()用作上下文来设置闹钟.
该onReceive()代码:
@Override
public void onReceive(Context context, Intent intent) {
for (SchedulerListener listener : listeners) {
listener.fetchAndRebuildNotification();
}
} …Run Code Online (Sandbox Code Playgroud) 它是一个24*7的跟踪应用程序.每当它停止我使用START_REDELIVER_INTENT重新启动它,但它不是每次都启动.它在应用程序管理器中显示以下类型响应 请建议.

我有一个前台服务从网上下载一些内容.
不幸的是,由于在这里报告的bug 我的服务在接收器收到我的服务中的广播时被杀死但它的通知不会被杀死我看到我的logcat中的以下日志:
I/ActivityManager(449): Killing 11073:my-package-name/u0a102 (adj 0): remove task
Run Code Online (Sandbox Code Playgroud)
无论如何在它的父服务被OS杀死时销毁前台通知?
我问了一个关于保持服务活着的问题,但我找不到解决方案,所以我有另一个更简单的问题.
android doc说如果android在低内存状态下返回onStartCommand时使用START_STICKY杀死一个服务,它将重新创建服务,如果我是正确的.
但是这段服务在一段时间后被杀死并在运行任务中消失但是没有重新创建!我在手机上运行Android 4.4.2中的这项服务,当屏幕打开时,它存活了大约20分钟但是当屏幕关闭时它在大约3或4分钟后消失...在我的平板电脑上(再次安装4.4.2)它停留的时间更长,大约4或5个小时,然后又消失了(我在不同的测试中得到了不同的结果).我甚至在android 5上测试它,结果类似于Android 4.4.2的平板电脑
我在这里错过了什么吗?当我们使用return START_STICKY直到我调用stopService时,我认为服务不会被破坏
这是我的服务:
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
super.onDestroy();
}
}
Run Code Online (Sandbox Code Playgroud)
sry for bad english :)