Android服务无法停止运行

时间:2016-05-03 14:53:25

标签: android android-activity android-service

我创建了一个服务并将其放在前台并附上通知。在服务中,我有一个计时器,只打印“SERVICE STILL RUNNING”,让我知道服务仍然存在。当我关闭应用程序时,服务的OnDestroy()被调用但是timmer继续打印“SERVICE STILL RUNNING”为什么会这样?我通过调用showNotification()将服务放在前台。如果我不调用showNotification()并关闭应用程序,服务将被销毁,并且timmer停止打印“SERVICE STILL RUNNING”。如何在应用程序关闭时将服务置于前台并正确终止它。即使应用程序关闭后,内存监视器仍会继续显示内存使用情况。如果我不把服务放在前台并关闭应用程序,内存监视器会停止。这个问题只发生在android 6.0上。

MainActivity:

public class MainActivity extends Activity {

    Intent service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        service = new Intent(MainActivity.this, MyService.class);

        // starting Client service
        service.setAction("START");
        startService(service);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        service.setAction("STOP");
        stopService(service);
    }
}

服务:

public class MyService extends Service {

    Timer timer;




    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
       //return super.onStartCommand(intent, flags, startId);
        if(intent !=null)
        {
            if(intent.getAction().equals("START"))
            {

               showNotification();
            } else if(intent.getAction().equals("STOP"))
            {
                stopForeground(true);
                stopSelf();

            }
        }else
        {
            //stopForeground(true);
            //stopSelf();

        }




        return START_STICKY;
    }


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

        timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run()
            {


                Log.d("","SERVICE STILL RUNNING");

            }
        },1*1000,5*1000);
    }

    private void showNotification() {
        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setAction("new");
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);


        Bitmap icon = BitmapFactory.decodeResource(getResources(),
                R.drawable.ic_launcher);

        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("Title")
                .setTicker("Test")
                .setContentText("testing")
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
                .setContentIntent(pendingIntent)
                .setOngoing(true).build();

        startForeground(101, notification);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("","SERVICE HAS BEEN DESTROYED!!!");
    }
}

1 个答案:

答案 0 :(得分:0)

尝试在Service's onDestroy上添加此内容

@Override
public void onDestroy() {
    super.onDestroy();
    timer.cancel();
    timer = null;
    stopForeground(true);//Add this. Since stopping a service in started in foreground is different from normal services.
    Log.d("","SERVICE HAS BEEN DESTROYED!!!");
}

修改

在您的Activity的onDestroy方法

@Override
protected void onDestroy() {
    super.onDestroy();
    service.setAction("STOP");
    service.executeStopForeground();
    stopService(service);
}

然后在您的服务上添加此方法:

public void executeStopForeground()
{
    stopForeground(true);
}