ENV: - Android 6.0,华为智能手机
我的应用是IM客户端,必须与服务器保持连接并接收 来自它的消息。所以我必须一直保持活着,只要它 网络连接可用。
我使用前台服务来保持我的应用程序在后台运行。请参阅:android-keeping-a-background-service-alive-preventing-process-death此方法适用于Android 4.1和4.4。但它在Android 5.1和6.0上失败了。 单击POWER按钮后,应用程序进程将被杀死大约10秒钟。
以下是重新制作的步骤。它是一个简单的演示应用程序,它启动一个线程来跟踪进程是被杀死还是活着。
覆盖服务onStartCommand()方法,调用startForeground()并显示 通知;
单击按钮以启动服务;
等待大约10秒钟,记录线程停止;
再次单击应用程序图标以启动应用程序;
注意:我注意到一件奇怪的事情:当应用程序再次启动时,不会调用活动的onCreate()!
感谢任何帮助!提前致谢!
这是我开始服务的代码:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
AppGlobal.logDebug(">>>> onStartCommand()");
if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
Log.i(LOG_TAG, "==== Received Start Foreground Intent ");
showNotification();
Toast.makeText(this, "Service Started!", Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) {
Log.i(LOG_TAG, "Clicked Previous");
Toast.makeText(this, "Clicked Previous!", Toast.LENGTH_SHORT)
.show();
} else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {
Log.i(LOG_TAG, "Clicked Play");
Toast.makeText(this, "Clicked Play!", Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) {
Log.i(LOG_TAG, "Clicked Next");
Toast.makeText(this, "Clicked Next!", Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(
Constants.ACTION.STOPFOREGROUND_ACTION)) {
Log.i(LOG_TAG, "==== Received Stop Foreground Intent");
stopForeground(true);
stopSelf();
}
return START_STICKY;
}
private void showNotification() {
AppGlobal.logDebug(">>>> show notification");
Intent notificationIntent = new Intent(this, StTest33MainActivity.class);
notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Intent previousIntent = new Intent(this, ForegroundService.class);
previousIntent.setAction(Constants.ACTION.PREV_ACTION);
PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,
previousIntent, 0);
Intent playIntent = new Intent(this, ForegroundService.class);
playIntent.setAction(Constants.ACTION.PLAY_ACTION);
PendingIntent pplayIntent = PendingIntent.getService(this, 0,
playIntent, 0);
Intent nextIntent = new Intent(this, ForegroundService.class);
nextIntent.setAction(Constants.ACTION.NEXT_ACTION);
PendingIntent pnextIntent = PendingIntent.getService(this, 0,
nextIntent, 0);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("TutorialsFace Music Player")
.setTicker("TutorialsFace Music Player")
.setContentText("My song")
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.setOngoing(true)
.addAction(android.R.drawable.ic_media_previous, "Previous",
ppreviousIntent)
.addAction(android.R.drawable.ic_media_play, "Play",
pplayIntent)
.addAction(android.R.drawable.ic_media_next, "Next",
pnextIntent).build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
notification);
}
答案 0 :(得分:0)
如果进程处于睡眠模式时,华为不会将其列为受保护的应用程序,则会终止进程。 转到设置 - >受保护的应用 - >选择您的应用并启用为受保护的应用。