Question

I want to start an Intent with an AlarmManager referring to code in an inner class.

My code in MainActivity.class

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
[...]
    DataProcessing2 dp = new DataProcessing2();

    startstop.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v1) {
            if(!listening){
                Log.i("Start","Button Pressed");
                listening = true;
                startstop.setText(R.string.stop);
                dp.start(getApplicationContext());
            }
        }
    });
}
}

My code in the DataProcessing2.class

public class DataProcessing2 {

public void start_accel(Context context){
    Log.i("Start Accel","Accessed");
    accel.setAccel(context);
    accel.listenAccel();

    Intent getAccelValuesIntent = new Intent(context, DataProcessing2.GetAccelValues.class);
    PendingIntent getValues = PendingIntent.getService(context, 0, getAccelValuesIntent, 0);
    getAccelData = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    getAccelData.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), sampling_acc_ms, getValues);
}

public class GetAccelValues extends IntentService {

    public GetAccelValues() {
        super("GetAccelValues");
        }

            [...]

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i("Acc Values","Intent received");
        accel.calculateAccelerometerData();
        getCalculatedData();
        boolean moving = sD.acc_delta > sD.acc_th;

        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction(NOTIFICATION_SERVICE);
        broadcastIntent.putExtra("name", "answerAccelerometer");
        broadcastIntent.putExtra("moving", moving);
        sendBroadcast(broadcastIntent);
    }

}

The problem is that I never get the receive the Intent on GetAccelValues.

Was it helpful?

Solution

The problem is that you're using incompatible clocks. You should either use AlarmManager.ELAPSED_REALTIME_WAKEUP together with System.elapsedRealtime(), or AlarmManager.RTC_WAKEUP together with System.currentTimeMillis(). For example:

getAccelData.setInexactRepeating(AlarmManager.RTC_WAKEUP,
        System.currentTimeMillis(), sampling_acc_ms, getValues);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top