Launch the Phonegap app at a specific time (alarm)

Ok, so I'm trying to launch the Phonegap App at a specific time, the same functionality as an alarm clock. To this end, I 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 alarm.java

  • Intent intent = new Intent(ctx, AlarmReciever.class); causes:

    Constructor Intent (CordovaInterface, Class) undefined alarm.java

  • PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT); causes:

    The getBroadcast (Context, int, Intent, int) method in the PendingIntent type is not applicable for the arguments (alarm, int, Intent, int) alarm.java`

My Java-File receiver is called AlarmReciever.java .

If someone sees what I'm doing wrong, I will be grateful for the help.

+6
source share
3 answers

Like @shaish, first create a link to Context so you can work with Android :.

 Context context = cordova.getActivity().getApplicationContext(); 

Use the context to create an Intent, create a PendingIntent, and receive system maintenance.

 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 problem is the AlarmReceiver class. It should extend 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 in AndroidManifest.xml:

  <application> ... <receiver android:name="de.ma.AlarmReceiver"/> </application> 

Alternative to BroadcastReceiver, if AlarmReceiver is an Activity, you can simply use:

 PendingIntent sender = PendingIntent.getActivity(context, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

which launches your activity directly.

+4
source

The code you used for the signaling service is intended to be used in context. you should get a context (perhaps using something like: Context ctx = cordova.getActivity (). GetApplicationContext ().)

and then to 1, use ctx.getSystemService, instead of 3 pass ctx instead, and instead of ALARM_SERVICE write Context.ALARM_SERVICE.

+2
source

You can do this using the background service, which initiates the application at a specific time.

-1
source

Source: https://habr.com/ru/post/919927/


All Articles