IntentService لزجة و استشعار مراقبة يتوقف عند ذلك لا يجب في الروبوت

StackOverflow https://stackoverflow.com/questions/9501163

سؤال

في تطبيق onCreate أنا التحقق من بعض الشروط ثم أبدأ في نشاط من هذا القبيل:

Intent startIntent = new Intent(getApplicationContext(), EnableLocationProviderActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(startIntent);

من هذا النشاط أبدأ IntentService الذي سجل بعض المستمعين لأجهزة الاستشعار, فمن بدأ لزجة وهذا يعني أنه يجب أن تتوقف صراحة.أن IntentService وترصد أجهزة الاستشعار.

مشكلتي هي أنني عندما أعود إلى أول نشاط أجهزة الاستشعار لا الاستشعار عن بعد (أنا وضعت السجل.v في onSensorChanged (بدء عرض البيانات ، ومن ثم يتوقف).

لماذا يمكن أن يتوقف إذا لم توقفت عن ذلك صراحة ؟ وعلاوة على ذلك أنا نرى في بعض الأحيان OnDestroy من IntentService يطلق, ولكن مرة أخرى, كيف يمكن أن يطلق إذا كان مثبت و أنا لم أتصل stopself() و لم تتوقف في أي طريقة أخرى ؟

وذلك بفضل!غييرمو.

تحرير

هذا هو رمز من IntentService (التي يجب أن تكون قيد التشغيل طوال الوقت على الرغم من إذا كان الهاتف المحمول يذهب إلى النوم أو الضغط على زر الصفحة الرئيسية (أنا أعرف عن البطارية و كل شيء آخر ، سيتم تحذير المستخدم عن هذا و سوف يكون تعلموها لإغلاق التطبيق عندما يريد.

الخدمة يتم استدعاؤها من MainActivity مثل هذا:

Intent startIntent = new Intent(GdpTesisApplication.getInstance().getApplicationContext(), SensingService.class);
startService(startIntent);

و رمز الخدمة هو هذا:

public class SensingService extends IntentService implements SensorEventListener {
    private float[] mAccelerationValues;
    private SensorManager mSensorManager = null;
    String sensorType = "";

    public SensingService(String name) {
        super(name);
        setIntentRedelivery(true);
    }

    public SensingService() {
        super("SensingService");
        setIntentRedelivery(true);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.v(ApplicationName,"SensingService.onStartCommand");
        super.onStartCommand(intent, flags, startId); // If this is not written then onHandleIntent is not called.
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.v(ApplicationName, "SensingService.onCreate");
        initialize();
    }

    private void initialize() {
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); // This must be in onCreate since it needs the Context to be created.
        mAccelerationValues = new float[3];

        Log.v(ApplicationName, "Opening Location Service from Sensing Service");
        LocationService myLocation = new LocationService();
        myLocation.getLocation(this, locationResult);
    }

    @Override
    public void onDestroy() {
        Log.v(ApplicationName, "SensingService.onDestroy");
        super.onDestroy();
        if (mSensorManager != null) {
            mSensorManager.unregisterListener(this);
        }
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.v(ApplicationName, "SensingService.onHandleIntent");
        if (mSensorManager != null) {
            registerListeners();
        }
    }

    public LocationResult locationResult = new LocationResult() {
        @Override
        public void gotLocation(final Location location) {
            if (location != null) {
                Log.v(ApplicationName, "Location != null : (" + location.getLatitude() + "," + location.getLongitude() + ")");
            } else {
                Log.v(ApplicationName, "Location == null : (0,0)");
            }
        }
    };

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    public void onSensorChanged(SensorEvent currentEvent) {
        if (currentEvent.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
            return;
        }

        synchronized (this) {
            float[] accelVals = null;
            float totalForce = 0.0f;

            int sensor = currentEvent.sensor.getType();
            System.arraycopy(currentEvent.values, 0, mAccelerationValues, 0, 3); // We use System.arraycopy because of this:
            switch (sensor) {
            case Sensor.TYPE_ACCELEROMETER:
                sensorType = "Accelerometer";
                totalForce = SensorsHelpers.getTotalForceInGs(mAccelerationValues); 
                break;
            case Sensor.TYPE_LINEAR_ACCELERATION:
                sensorType = "LinearAcceleration";
                totalForce = SensorsHelpers.getTotalForceInGs(mAccelerationValues) + 1; 
                break;
            case Sensor.TYPE_GRAVITY:
                totalForce = SensorsHelpers.getTotalForceInGs(mAccelerationValues); 
                sensorType = "Gravity";
                break;
            } 
            Log.v(ApplicationName,DateHelper.GetUTCdatetimeFromDate(new Date()) + " - from sensingService");
        }
    }

    private void registerListeners() {
        Log.v(ApplicationName, "Registering sensors listeners");
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION), SensorManager.SENSOR_DELAY_UI);
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),SensorManager.SENSOR_DELAY_UI);
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
    }
}

تحديث 2

الآن لقد أضاف هذا على طريقة onCreate:

int NOTIFICATION_ID = 1;
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 1, intent, 0);
Notification notification = new Notification(R.drawable.ic_dialog_info, "Running in the Foregound", System.currentTimeMillis());
notification.setLatestEventInfo(this, "Title", "Text", pi);
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
startForeground(NOTIFICATION_ID, notification);

لبدء كما startForground ، ولكنه يضع أيقونة في شريط الإعلام ، ثم onDestroy يسمى في الخدمة و رمز الإعلام يذهب بعيدا.

أنا يائسة الآن!الرجاء المساعدة في هذا واحد!

وذلك بفضل!غييرمو.

هل كانت مفيدة؟

المحلول 2

حسنا، لقد رأيت الرد على سؤال آخر، وهناك رجل قال إنه خطأ في نظام Android، تابعت اقتراحه لنقل الرمز إلى OnCreate بدلا من OnhandLeintent ويعمل!لذلك، إذا لم يكن هناك أحد يعرض لي، فهذه مشكلة في التعليمات البرمجية، بالنسبة لي سيكون خطأ.شكرا!

نصائح أخرى

كما في IntentService الوثائق:

الخدمة بدأت حسب الحاجة ، يعالج كل نية بدوره باستخدام عامل الموضوع ، وتوقف نفسها عندما ينتهي من العمل

أيضا, وفقا لنفس الوثائق أنت ليس من المفترض أن تجاوز onStartCommand() و onDestroy() في IntentService, أفترض لأنه ينفذ الخاصة به السلوك على النحو المحدد أعلاه.ربما تحتاج إلى تمديد Service بدلا من IntentService.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top