Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid AlarmManager for periodical sensor reading
    primarykey
    data
    text
    <p>I have a task to periodically read the phone sensors (e.g. WiFi, accelerometer) in the backend.</p> <p>My current solution is to use an AlarmManager.</p> <p>Specifically, we have:</p> <p>In the "main" program (an activity), we use PendingIntent.getService:</p> <pre> public class Main extends Activity { ... Intent intent = new Intent(this, AutoLogging.class); mAlarmSender = PendingIntent.getService(this, 0, intent, 0); am = (AlarmManager)getSystemService(ALARM_SERVICE); am.setRepeating(AlarmManager.RTC, 0, 5*1000, mAlarmSender); } </pre> <p>In the "AutoLogging" program (a service), we respond to the alarm periodically:</p> <pre> public class AutoLogging extends Service { ... @Override public void onCreate() { Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show(); } @Override public void onDestroy() { super.onDestroy(); Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show(); } @Override public boolean onUnbind(Intent intent) { Toast.makeText(this, "onUnbind", Toast.LENGTH_SHORT).show() return super.onUnbind(intent); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Toast.makeText(this, "onStart", Toast.LENGTH_SHORT).show(); // Read sensor data here } @Override public IBinder onBind(Intent intent) { Toast.makeText(this, "onBind", Toast.LENGTH_SHORT).show(); return null; } } </pre> <p>My problem is:</p> <p>When I use this alarm service, only OnCreate and OnStart are called at each alarm. </p> <p>My questions are:</p> <p>(1) Do we need to call OnDestroy (or onBind, onUnbind)?</p> <p>(2) Is this a correct way to use AlarmManager (compared with "broadcase receiver")? </p> <p>Thanks! Vincent</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload