好的,所以我试图让我的 Phonegap 应用程序在特定时间启动,与闹钟功能相同。为此,我尝试编写自己的插件:
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;
}
}
但是编译时出现3个错误:
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
原因:ALARM_SERVICE 无法解析为变量 alarm.java
Intent intent = new Intent(ctx, AlarmReciever.class);
原因:构造函数 Intent(CordovaInterface, Class) 未定义 alarm.java
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
原因:PendingIntent 类型中的方法 getBroadcast(Context, int, Intent, int) 不适用于参数 (alarm, int, Intent, int) alarm.java`
我的接收器 Java-File 被称为AlarmReciever.java
.
如果有人看到我做错了什么,那么我将不胜感激。