Ok, so I'm trying to make my Phonegap App start at a specific time, same functionality as an alarm clock. To this end i've tried to write my own plugin:
package de.ma;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import android.content.Intent;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.os.Bundle;
import java.util.Calendar;
import android.os.SystemClock;
import com.phonegap.api.Plugin;
public class alarm extends Plugin {
@Override
public PluginResult execute(String arg0, JSONArray arg1, String arg2) {
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.SECOND, 5);
Intent intent = new Intent(ctx, AlarmReciever.class);
intent.putExtra("alarm_message", "O'Doyle Rules!");
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
return null;
}
}
But when compiling I get 3 errors:
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
causes:
ALARM_SERVICE cannot be resolved to a variable alarm.java
Intent intent = new Intent(ctx, AlarmReciever.class);
causes:
The constructor Intent(CordovaInterface, Class) is undefined alarm.java
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
causes:
The method getBroadcast(Context, int, Intent, int) in the type PendingIntent is not applicable for the arguments (alarm, int, Intent, int) alarm.java`
My receiver Java-File is called AlarmReciever.java
.
If anyone sees what I'm doing wrong then I'd appreciate the help.
Like @shaish said, first to create a Context reference so that you can do Android operation:.
Context context = cordova.getActivity().getApplicationContext();
Use the context to create Intent, create PendingIntent and acquire system service.
Intent intent = new Intent(context, AlarmReciever.class);
...
PendingIntent sender = PendingIntent.getBroadcast(context, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
...
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Another issue is the AlarmReceiver class. It should be extending BroadcastReceiver:
public class AlarmReceiver extends BroadcastReceiver {
private static final String LOG_TAG = AlarmReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.d(LOG_TAG, "onReceive()");
}
}
And you need to put it into AndroidManifest.xml:
<application>
...
<receiver android:name="de.ma.AlarmReceiver"/>
</application>
Alternative to a BroadcastReceiver, if AlarmReceiver is an Activity, you can just use:
PendingIntent sender = PendingIntent.getActivity(context, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
which launches your Activity directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With