前台服务因执行与互联网相关的操作而被杀 [英] Foreground service gets killed on performing internet related operations

查看:51
本文介绍了前台服务因执行与互联网相关的操作而被杀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:以前,我找不到关于何时终止前台服务的明确定义的模式.在对发生这种情况的设备进行了更多调试(并非全部发生)之后.

1.)很多时候,我打开chrome加载网站时,前台服务都被杀死.有时甚至在我使用whatsapp时,也会发生这种情况.

2.)没有例外,并且堆栈跟踪没有显示任何有用的信息.

以下原始问题:

在StackOverflow上有很多这样的问题,但是到目前为止,我已经阅读的大部分答案都表明,这取决于Android,并且我们不能100%保证不会终止前台服务.一些答案表明START_STICKY,但这对我而言并没有太大帮助.

There are many such questions on StackOverflow but the answers so far that I have read mostly say that it is upto Android and we don't have 100% guarantee that a foreground service will not be killed. Some answers suggest START_STICKY but that is not much helpful in my case.

就我而言,我有一个具有前台服务的音乐播放器应用程序.此服务在某些设备上被杀死,大部分是Xiomi的某些版本(Android版本为5.1.1). 现在,我了解到android可能内存不足,因此我的前台服务被杀死了,但是为什么其他音乐播放器应用程序从未经历过这种终止.他们在做我不对的事情是什么?

In my case I have a music player app which has a foreground service. This service gets killed on certain devices, mostly some versions of Xiomi (Android version was 5.1.1). Now I understand that android might be short on memory and so my foreground service is being killed, but then why do other music player apps never go through such termination. What is it that they are doing right that I am not?

我使用startForeground进行了服务前台服务.另外,我在onStartCommand中返回START_STICKY,尽管这无济于事,因为该服务在终止后的4-5秒后会重新启动.为了将服务与活动绑定在一起,我使用

I made my service foreground service by using startForeground. Also I return START_STICKY in onStartCommand although that doesn't help because the service is restarted after a period of 4-5 sec if killed. To bind my service with my activity I use

bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT );

那么我该如何改进/更改我的应用程序以防止这种情况发生,如果其他应用程序正常运行,那么我的情况肯定有问题.有人可以帮忙吗?在此先感谢!

So what exactly can I improve/change in my app to prevent this from happening, if other apps are working right there must be something that is wrong in my case. Can someone please help. Thanks in advance !!

这就是我所谓的startForeground()

This is how I call startForeground()

public void sendNotification() {

        Intent notIntent = new Intent(this, MainActivity.class);
        notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendInt = PendingIntent.getActivity(this, 0,
                notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Bitmap bitmap = null;
        if (!notificationShowing || !forwarded) {
            Log.i(TAG, "present");
            String title = CommonUtils.getSongFromID(songIndex, this);

            bigView.setTextViewText(R.id.title, title);
            bigView.setImageViewBitmap(R.id.img, bitmap);

            smallView.setTextViewText(R.id.title1, title);
            smallView.setImageViewBitmap(R.id.img1, bitmap);

            if (pauseButton == 1) {
                bigView.setImageViewResource(R.id.pause, R.drawable.pause_noti);
                smallView.setImageViewResource(R.id.pause1, R.drawable.pause_noti);
            } else {
                bigView.setImageViewResource(R.id.pause, R.drawable.play_noti);
                smallView.setImageViewResource(R.id.pause1, R.drawable.play_noti);
            }

            musicNotification = builder.setContentIntent(pendInt)
                    .setSmallIcon(R.drawable.logo1)
                    .setTicker(songTitle)
                    .setOngoing(true)
                    .setContentTitle("Playing")
                    .setStyle(new Notification.BigTextStyle().bigText("Song App"))
                    .setContentText(songTitle)
                    .setPriority(Notification.PRIORITY_MAX)
                    .build();

            musicNotification.contentView = smallView;
            musicNotification.bigContentView = bigView;

            musicNotification.contentIntent = pendInt;

            Intent switchIntent = new Intent("pause");
            switchIntent.putExtra("button", "pause");
            PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 100, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            bigView.setOnClickPendingIntent(R.id.pause, pendingSwitchIntent);
            smallView.setOnClickPendingIntent(R.id.pause1, pendingSwitchIntent);

            Intent switchIntent1 = new Intent("forward");
            switchIntent1.putExtra("button", "forward");
            PendingIntent pendingSwitchIntent2 = PendingIntent.getBroadcast(this, 100, switchIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
            bigView.setOnClickPendingIntent(R.id.forward, pendingSwitchIntent2);
            smallView.setOnClickPendingIntent(R.id.forward1, pendingSwitchIntent2);

            Intent switchIntent2 = new Intent("previous");
            switchIntent2.putExtra("button", "previous");
            PendingIntent pendingSwitchIntent3 = PendingIntent.getBroadcast(this, 100, switchIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
            bigView.setOnClickPendingIntent(R.id.previous, pendingSwitchIntent3);
            smallView.setOnClickPendingIntent(R.id.previous1, pendingSwitchIntent3);

            Intent switchIntent3 = new Intent("end");
            switchIntent3.putExtra("button", "end");
            PendingIntent pendingSwitchIntent4 = PendingIntent.getBroadcast(this, 100, switchIntent3, PendingIntent.FLAG_UPDATE_CURRENT);
            bigView.setOnClickPendingIntent(R.id.end, pendingSwitchIntent4);
            smallView.setOnClickPendingIntent(R.id.end1, pendingSwitchIntent4);

            startForeground(NOTIFY_ID, musicNotification);
            notificationShowing = true;
        }
        forwarded = false;

    }

推荐答案

由于以下原因,这发生在 Xiomi 手机中.

This happened in Xiomi phone due to below reason.

MIUI 7.0解决方案=>安全=>自动启动=>选择要在后台运行的应用=>重新启动 重新启动后,您的设备应该能够像其他android设备一样在后台运行您的应用程序服务.

Solution for MIUI 7.0 => Security => Autostart => select Apps that you want to run in background => Reboot After reboot your device should able to run your application services in background like other android devices do.

MIUI 4.0设置

MIUI自动启动详细说明

如果您正在寻找其他电话,请检查此处是服务结构.它会自动重新启动,但在您重新启动BootReceiver时会重新启动电话.

And if you looking for other phone then check here is service structure.It automatically restart but when you restart phone call BootReceiver.

public class AppService extends Service {

private class LocalBinder extends Binder {
    public AppService getServerInstance() {

        return AppService.this;
    }
}


@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // If we get killed, after returning from here, restart

    return Service.START_STICKY;
}

@Override
public void onCreate() {
    super.onCreate();

} 

@Override
public void onDestroy() {

}

}

感谢希望能对您有所帮助.

Thanks hope this will help you.

这篇关于前台服务因执行与互联网相关的操作而被杀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆