My friend and I are teaching ourselves android by programming a toy app, literally. We made a simple game that uses the accelerometer. The game is just a maze game with a ball. We use the accelerometer to detect movement and use tile detections to determine whether the ball is on a path or hits a wall.
However, we want to build a timing service with the accelerometer as well. This timing service would determine when the user is being idle using the accelerometer (like 10 secs), issue a warning, then turn off the game. That is, kill the activity.
So what I am doing now is to see if I can throw up a message within 10 seconds of the game running on the widget. I am basing this exactly off the code found here:
I implemented the code found on that tutorial above as follows in my view below. I created a method called timingService and called it in my toyappView:
public void timingService(Context context, Activity activity){
// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add 10 seconds to the calendar object
cal.add(Calendar.SECOND, 10);
Intent intent = new Intent(context, AlarmReciever.class);
intent.putExtra("alarm_message", "This is the alarm message");
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(context, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) activity.getSystemService(context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
Here is the class it should be calling when the alarm goes off.
package com.example.toyapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class AlarmReciever extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
try{
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm message");
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(context, "There was an error somewhere", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
After here I just get stuck again. My AlarmReciever from the tutorial is never being called. Why? I ran debug with my emulator and it never goes into the AlarmReciever. As far as I am concerned, this game should go for 10 seconds and then call the AlarmReciever and display a message on my widgit. Right? I suspect my problem is in am.set? But I don't see why.
As always, thank you all for your patience. I appreciate your comments and insights as I teach myself how to write a toy app in android. This may seem silly to others, but I already put a lot of thought in this. So I would appreciate any help others would have to offer. Hope you let me off on that technicality. Also, I have a cold. I don't believe this duplicates other's problems on stackoverflow as this alarm is going to be modified for a accelerometer. I will change it from making a toast message to killing the game.
Warm Regards, GeekyOmega
PS - Some similar issues around, but found nothing that addressed why my class is not being called. I have a really bad cold too, so please let me off on that technicality. I'm trying. :-)
I would recommend using the AlarmManager. Every time your onSensorChanged(...) gets called you reset an alarm that will end your game in 10 seconds.