【问题标题】:Android Background Service stop in lollipop whille swipe from task Manager从任务管理器滑动时,Android 后台服务在棒棒糖中停止
【发布时间】:2016-08-08 06:16:01
【问题描述】:

我在 android 应用程序中有一项服务,我想在后台连续运行,即使应用程序从任务管理器中被杀死。我当前的代码适用于棒棒糖之前的版本。但在棒棒糖中它不起作用(在棒棒糖中它停止来自任务管理器)。如何解决这个问题。

服务类

public class MyService extends Service {

    int total_time = 1000 * 60 * 60 * 24; // total one day you can change
    int peroid_time = 10000; // one hour time is assumed to make request

@Override
    public void onCreate() {

    }

    /** The service is starting, due to a call to startService() */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {


        new CountDownTimer(total_time, peroid_time) {


            public void onTick(long millisUntilFinished) {
                // make request to web and get reponse and show notification.
                MakingWebRequest();

                Toast.makeText(MyService.this, " Tesitng the data", Toast.LENGTH_LONG).show();
            }


            public void onFinish() {

                Toast.makeText(MyService.this, " ending............", Toast.LENGTH_LONG).show();
                //
            }
        }.start();
        // Let it continue running until it is stopped.
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        return START_STICKY;

    }

    public void MakingWebRequest() {

                                NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                                NotificationCompat.Builder builder = new NotificationCompat.Builder(MyService.this);
                                Notification notify ;
                                PendingIntent pending = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);

                                notify = builder.setContentIntent(pending)
                                        .setStyle(new NotificationCompat.BigTextStyle().bigText(message1))
                                        .setSmallIcon(R.drawable.push).setTicker(excep).setWhen(System.currentTimeMillis())
                                        .setAutoCancel(true).setContentTitle(message1)
                                        .setContentText(message1).build();
//                notif.notify(NOTIFICATION, notify);
//                                notif.notify(0, notify);
                                    startForeground(1, notify);

        }



    }

    /** A client is binding to the service with bindService() */
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    /** Called when all clients have unbound with unbindService() */
    @Override
    public boolean onUnbind(Intent intent) {
        return mAllowRebind;
    }

    /** Called when a client is binding to the service with bindService()*/
    @Override
    public void onRebind(Intent intent) {

    }

主要活动

Intent serviceIntent = new Intent(this, MyService.class);
        serviceIntent.setPackage(this.getPackageName());
        startService(serviceIntent);

【问题讨论】:

  • 所以你想每小时做一次?您不需要为此提供持续的服务。你每小时启动一个服务。 Google GcmNetworkManager 和 PeriodicTask。
  • 我不希望 gcm 之类的东西..请留在我的代码和逻辑上,只是如何防止停止应用程序
  • 你无法阻止它。我为您提供了一个替代方案,这也恰好是最佳实践。 GCM != GcmNetworkManager。 GcmNetworkManager = JobScheduler,这正是你所需要的。当网络可用时,它会定期运行任务(如果您设置了它)。是的,有时我们必须重写代码。

标签: android android-service android-5.0-lollipop android-background android-task


【解决方案1】:

这是一个 Android 错误。

请参阅https://groups.google.com/forum/#!topic/android-developers/mRDeMHVVS3Qhttps://code.google.com/p/android/issues/detail?id=53313#c1,建议使用onTaskRemoved() 解决方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    相关资源
    最近更新 更多