0

アプリケーションonCreateで、いくつかの条件を確認してから、次のようなアクティビティを開始します。

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

そのアクティビティから、センサーのリスナーを登録するIntentServiceを開始します。これは、STICKYとして開始されます。つまり、明示的に停止する必要があります。そのIntentServiceはセンサーを監視します。

私の問題は、最初のアクティビティに戻ると、センサーがセンサーを検出しなくなったことです(Log.vをonSensorChangedに入れました(データの表示を開始してから停止します)。

明示的に停止しなかった場合、なぜ停止するのでしょうか。さらに、IntentServiceのOnDestroyが呼び出されることもありますが、これも、STICKYで、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が呼び出され、通知アイコンが消えます。

今必死です!これで助けてください!

ありがとう!ギレルモ。

4

2 に答える 2

8

IntentServiceのドキュメントに従って:

サービスは必要に応じて開始され、ワー​​カー スレッドを使用して各インテントを順番に処理し、作業がなくなると停止します。

また、同じドキュメントによると、オーバーライドすることは想定されておらず、onStartCommand()上記onDestroy()IntentServiceように独自の特別な動作を実装しているためと思われます。Serviceの代わりに拡張する必要があるかもしれませんIntentService

于 2012-02-29T14:59:49.303 に答える
-2

わかりました、別の質問への回答を見ました。それは Android のバグだと言った人がいます。コードを onHandleIntent ではなく onCreate に移動するという彼の提案に従いました。したがって、誰も私にそれが私のコードの問題であることを示していない場合、私にとってはバグになります。ありがとう!

于 2012-03-04T00:15:23.763 に答える