Herb
unread,Nov 14, 2011, 12:08:49 PM11/14/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to phonegap
Hi group,
I could really use some help pulling together an app I am developing.
I haven't found examples that quite cover what I am doing, and many of
the non-Phonegap examples take things in directions that stretch my
limited knowledge of java. Once I get this done, I am sure others
will benefit from seeing it.
I want in this app to do a few things:
1. accept some alarm preference values from the javascript app and
save them as sharedpreferences.
2. use those values with alarmmanager to create a recurring alarm
3. set up a pending intent from the alarm just created
4. set up a broadcast receiver to wait for the intent
5. retrieve the shared preferences and fire the alarm is the
conditions are met.
My confusion has to do with not understanding how to structure the
various java classes and hook them up with Droidgap, Plugin, etc.
This is the plugin code:
<<<
public class PopaPlugin extends Plugin {
public final String ACTION_REFRESH="refreshAlarms";
public final String ACTION_SENDSMS="sendSMS";
@Override
public PluginResult execute(String action, JSONArray data, String
callbackId) {
PluginResult result = new PluginResult(Status.INVALID_ACTION);
if (ACTION_SENDSMS.equals(action)) {
try {
String phoneNumber = data.getString(0);
String message = "";
sendSMS(phoneNumber, message);
result = new PluginResult(Status.OK);
}
catch (JSONException e) {
result =new PluginResult(Status.ERROR, e.getMessage());
}
}
else if(ACTION_REFRESH.equals(action) ) {
try {
String on_off=data.getString(0);
String alert_type=data.getString(1);
int start_hour=data.getInt(2);
int end_hour = data.getInt(3);
int interval=data.getInt(4);
//save preferences
SharedPreferences alarmSettings =
ctx.getSharedPreferences("AlarmPreferences", 0);
SharedPreferences.Editor prefEditor = alarmSettings.edit();
prefEditor.putString("on_off", on_off);
prefEditor.putString("alert_type", alert_type);
prefEditor.putInt("start_hour",start_hour);
prefEditor.putInt("end_hour",end_hour);
prefEditor.putInt("interval",interval);
prefEditor.commit();
// RegisterAlarms
AlarmManager am = ( AlarmManager )
ctx.getSystemService( "ALARM_SERVICE" );
Intent intent = new Intent( "REFRESH_ALARM" );
PendingIntent pi = PendingIntent.getBroadcast( this.ctx, 0,
intent, 0 );
int repeatinterval = interval*60000;
am.setRepeating( AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), repeatinterval, pi );
result = new PluginResult(Status.OK);
}
catch (JSONException e) {
result =new PluginResult(Status.ERROR, e.getMessage());
}
}
return result;
}
private void sendSMS(String phoneNumber, String message) {
SmsManager manager = SmsManager.getDefault();
PendingIntent sentIntent = PendingIntent.getActivity(this.ctx, 0,
new Intent(), 0);
manager.sendTextMessage(phoneNumber, null, message, sentIntent,
null);
}
}
>>>
I think the plugin itself is OK, but I get confused when trying to do
things beyond the plugin - not knowing how Phonegap behaves with other
classes.
So, I have a Broadcast Receiver. Can I do that - Droidgap+Plugin
+_BroadcastReceiver? Where in the manifest do I register the
receiver - within Droidgap?
Now, I am wondering also - do I play the sound from the
BroadcastReceiver or do I launch a separate Service? Can Droidgap
live with a separate Service?
I also want to create a second receiver that waits for BOOT_COMPLETED
and reads the same sharedpreferences and recreates the alarms that
were lost at shutdown. Do I just add that second BroadcastReceiver to
the package?
<<<
public class AlarmReceiver extends BroadcastReceiver {
public final String ALERTS_ON="Y";
public final String ALERTS_SOUND="Y";
public final String ALERTS_VIBRATE="Y";
public final String ALERTS_BOTH="Y";
private int soundID;
// private SoundPool soundPool;
// boolean loaded = false;
@Override
public void onReceive( Context context, Intent MyIntent ) {
//get preferences
SharedPreferences alarmSettings =
context.getSharedPreferences("AlarmPreferences", 0);
String on_off=alarmSettings.getString("on_off", "0");
String alert_type=alarmSettings.getString("alert_type","0");
int start_hour=alarmSettings.getInt("start_hour",0);
int end_hour = alarmSettings.getInt("end_hour",0);
if (ALERTS_ON.equals(on_off)){
Calendar rightNow = Calendar.getInstance();
int HOUR_NOW = rightNow.get(Calendar.HOUR_OF_DAY);
if ((end_hour>=HOUR_NOW) && (start_hour<=HOUR_NOW)) {
if (ALERTS_SOUND.equals(alert_type)){
// Intent playIntent = new Intent( context, PlayService.class );
// context.startService(playIntent );
}
else if (ALERTS_VIBRATE.equals(alert_type)){
}
else if (ALERTS_BOTH.equals(alert_type)){
}
else {
}
}
}
}
}
>>>
Any help or link to an existing resource is much appreciated.