未绑定被毁服务,同时活动被杀害 [英] Unbound services destroyed while activity is killed

查看:250
本文介绍了未绑定被毁服务,同时活动被杀害的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建应用程序时被杀害,所以我创建了一个非绑定服务,以便它不会绑定到生命周期的活动,甚至是运行的服务。但每次我杀了长preSS和刷卡应用,服务也被打死。一些可以帮助我。
谢谢

服务

 公共类的MyService扩展服务{    公共GCMNotificationIntentService(){
    }    @覆盖
    公众的IBinder onBind(意向意图){
        Log.d(TAG,服务绑定);
        返回null;
    }    @覆盖
    公共布尔onUnbind(意向意图){
        Log.d(TAG,服务绑定);
        返回super.onUnbind(意向);
    }    @覆盖
    公共无效的onCreate(){
        Log.d(TAG,服务创造);
        super.onCreate();
    }    @覆盖
    公共无效的onDestroy(){
        Log.d(TAG,服务destoryed);
        super.onDestroy();
    }    @覆盖
    公众诠释onStartCommand(意向意图,诠释标志诠释startId){        Log.d(TAG,服务启动);        螺纹readerThread =新主题(新的Runnable(){
            @覆盖
            公共无效的run(){                而(真){
                    Log.d(TAG,线程present);
                    尝试{
                        //线程睡眠1000毫秒
                        视频下载(3000);
                    }赶上(例外五){
                        的System.out.println(E);
                    }
                }
            }
        });
        readerThread.start();        返回super.onStartCommand(意向,旗帜,startId);
    }}

和我从这样的活动称之为

startService(新意图(MyService.class.getName()));

该服务运行正常,直到应用程序在背景或前景,但是当我长
preSS和扫应用,服务也将停止,像这样的崩溃信息

在1000毫秒坠毁服务com.net.gs.MyService调度重启

I / ActivityManager(1160):强制停止服务ServiceRecord {44989bf0 U0 com.net.gs.MyService}


解决方案

  //在通知栏使用下面的功能显示粘通知。
私有静态最终诠释NOTIFICATION_ID = 1; 私人无效displayNotification(){
        NotificationCompat.Builder建设者=新NotificationCompat.Builder(
                这一点).setSmallIcon(R.drawable.ic_launcher).setContentTitle(
                玩家);
        builder.setContentText(内容文字);        如果(getFileStructure()!= NULL){
            字符串title = Utils.filter(getFileStructure()的getTitle());
            如果(title.length()> 45)
                标题= title.substring(0,44);
            builder.setContentText(Utils.filter(职称));
        }
        意图resultIntent =新意图(PlayerService.this,MainActivity.class);
        的PendingIntent resultPendingIntent = PendingIntent.getActivity(
                PlayerService.this,0,resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);
        builder.setAutoCancel(假);
        NotificationManager nManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        nManager.notify(NOTIFICATION_ID,builder.build());
        startForeground(NOTIFICATION_ID,builder.build());
    }

I want to create a service that is running even when app is killed so I created a unbound service so that it is not bind to activity lifecycle. But everytime I kill the app by long press and swipe, the service also get killed. Can some please help me. Thanks

Service

public class MyService extends Service {

    public GCMNotificationIntentService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "Service binded");
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "Service unbound");
        return super.onUnbind(intent);
    }

    @Override
    public void onCreate() {
        Log.d(TAG, "Service created");
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "Service destoryed");
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.d(TAG, "Service started");

        Thread readerThread = new Thread(new Runnable() {
            @Override
            public void run() {

                while(true) {
                    Log.d(TAG, "Thread present");
                    try {
                        // thread to sleep for 1000 milliseconds
                        Thread.sleep(3000);
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                }
            }
        });
        readerThread.start();

        return super.onStartCommand(intent, flags, startId);
    }

} 

and I am calling it from activity like this

startService(new Intent(MyService.class.getName()));

The service runs fine until app is in background or foreground but when I long press and sweep the app, service also stops with crash message like this

Scheduling restart of crashed service com.net.gs.MyService in 1000ms

I/ActivityManager( 1160): Force stopping service ServiceRecord{44989bf0 u0 com.net.gs.MyService}

解决方案

// Display sticky notification using below function in notification-bar.   
private static final int NOTIFICATION_ID=1;

 private void displayNotification() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher).setContentTitle(
                "Player");
        builder.setContentText("content text");

        if (getFileStructure() != null) {
            String title = Utils.filter(getFileStructure().getTitle());
            if (title.length() > 45)
                title = title.substring(0, 44);
            builder.setContentText(Utils.filter(title));
        }
        Intent resultIntent = new Intent(PlayerService.this, MainActivity.class);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(
                PlayerService.this, 0, resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);
        builder.setAutoCancel(false);
        NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nManager.notify(NOTIFICATION_ID, builder.build());
        startForeground(NOTIFICATION_ID, builder.build());
    }

这篇关于未绑定被毁服务,同时活动被杀害的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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