android Plugin+Broadcast Receiver+AlarmManager+SoundPool

1,516 views
Skip to first unread message

Herb

unread,
Nov 14, 2011, 12:08:49 PM11/14/11
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.

Simon MacDonald

unread,
Nov 15, 2011, 9:10:53 AM11/15/11
to phon...@googlegroups.com
Look at:


for an example of using a BroadcastReceiver in a plugin. 

Methinks you are trying to do too much in one plugin and you may want to split this functionality up or have the plugin call some separate classes to do this work.

Simon Mac Donald
http://hi.im/simonmacdonald



--
You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en

For more info on PhoneGap or to download the code go to www.phonegap.com

Herb

unread,
Nov 15, 2011, 11:55:00 AM11/15/11
to phonegap
Simon,

I now have it split up into the plugin and 4 more classes.

From what you write I gather that there is no Phonegap 'sandbox'
within the android app sandbox.

I can, for example, use sharedpreferences to share key-value pairs
with additional classes - receivers, services elsewhere in the
package.

I can re-use a service that is started by a plugin by also starting it
in another class that waits for a system event, for example.

Is this correct?

Herb

On Nov 15, 9:10 am, Simon MacDonald <simon.macdon...@gmail.com> wrote:
> Look at:
>
> https://github.com/callback/callback-android/blob/master/framework/sr...
>
> for an example of using a BroadcastReceiver in a plugin.
>
> Methinks you are trying to do too much in one plugin and you may want to
> split this functionality up or have the plugin call some separate classes
> to do this work.
>
> Simon Mac Donaldhttp://hi.im/simonmacdonald

Simon MacDonald

unread,
Nov 15, 2011, 12:50:22 PM11/15/11
to phon...@googlegroups.com
On Tue, Nov 15, 2011 at 11:55 AM, Herb <herba...@gmail.com> wrote:
> Simon,
>
> I now have it split up into the plugin and 4 more classes.
>
> From what you write I gather that there is no Phonegap 'sandbox'
> within the android app sandbox.

No, there is no PhoneGap sandbox. There is only the Android per
application sandbox.

> I can, for example, use sharedpreferences to share key-value pairs
> with additional classes - receivers, services elsewhere in the
> package.

Yes

> I can re-use a service that is started by a plugin by also starting it
> in another class that waits for a system event, for example.

I haven't done it personally but it seems correct to me.

Reply all
Reply to author
Forward
0 new messages