代码之家  ›  专栏  ›  技术社区  ›  Javier Enríquez

某些设备中的Android复制服务

  •  0
  • Javier Enríquez  · 技术社区  · 10 年前

    我正在开发一个简单的计时器应用程序,它使用服务(这是为了让计时器保持运行,即使应用程序已经关闭)。

    计时器逻辑由服务处理,我的Fragment只是绑定到服务来获取数据。我的问题是,当我启动计时器并关闭应用程序时。当我再次打开它时,计时器绑定到服务的新实例,因此它认为它没有运行。

    这只发生在我测试的一个设备中(在模拟器中,其他4个设备工作正常)。这到底是怎么回事??

    以下是一些代码:

    碎片 onStart :

    public void onStart() {
        super.onStart();
        Intent intent = createServiceIntent();
        getActivity().startService(intent);
        getActivity()
            .bindService(
                intent,
                mServiceConnection,
                Context.BIND_ABOVE_CLIENT
            );
    
    }
    

    服务构造函数, onCreate , 启动时 , onBind 和 onUnbind

    public ChronometerService() {
        Log.d(Config.APP_TAG, "CREATING CHRONOMETER SERVICE INSTANCE");
    }
    
    @Override
    public void onCreate()
    {
        super.onCreate();
        this.mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        this.mBinder = new ChronometerBinder();
        this.mPauses = new LinkedList<>();
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        return START_STICKY;
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(Config.APP_TAG, "SERVICE BIND. AND WAS" + (mRunning ? "" : "N'T") + " RUNNING");
        if( ! mRunning ) {
            this.mPauseLength = 0;
            this.mStoppedAt = 0;
            this.mBase = 0;
        }
        return mBinder;
    }
    
    @Override
    public boolean onUnbind(Intent intent) {
        if( ! mRunning) {
            stopForeground(true);
            stopSelf();
        }
        return true;
    }
    

    当点击片段中的启动按钮时,将调用服务中的以下方法:

    public void start() {
        if(this.mBase == 0) {
            reset();
            this.mBase = now();
        }else{
            mPauseLength += now() - mStoppedAt;
        }
        Intent i = new Intent(this, MainActivity.class);
        i.putExtra(MainActivity.CURRENT_FRAGMENT, MainActivity.CHRONOMETER_TAG);
        PendingIntent intent = PendingIntent.getActivity(this, SERVICE_ID, i, PendingIntent.FLAG_UPDATE_CURRENT);
    
        mNotificationBuilder = new NotificationCompat.Builder(this);
        mNotificationBuilder.setContentTitle(getResources().getString(R.string.chronometer_title))
            .setSmallIcon(R.drawable.ic_timer_white_48dp)
            .setContentIntent(intent);
    
        Notification n = mNotificationBuilder.build();
        startForeground(SERVICE_ID, n);
        mRunning = true;
        update();
    }
    

    在应用程序运行正常的设备中,在日志中我得到:

    CREATING CHRONOMETER SERVICE INSTANCE
    SERVICE BIND. AND WASN'T RUNNING
    (Closing the app)
    SERVICE BIND. AND WAS RUNNING
    

    在设备中它不起作用,我得到:

    CREATING CHRONOMETER SERVICE INSTANCE
    SERVICE BIND. AND WASN'T RUNNING
    (Closing the app)
    CREATING CHRONOMETER SERVICE INSTANCE
    SERVICE BIND. AND WASN'T RUNNING
    

    希望你能帮助我。

    1 回复  |  直到 10 年前
        1
  •  0
  •   Javier Enríquez    10 年前

    解决了的:

    问题毕竟不是我的代码。问题是 Purify

    这会毁了我的服务。将我的应用程序添加到白名单后,问题就解决了。