我使用PhoneB的StatusBarNotification插件(Android)來觸發通知。現在我想在特定的時間對此進行說明,從我閱讀的內容中我必須使用Android的AlarmManager。我嘗試了一些方法,但似乎無法使其工作。AlarmManager與phonegap
有關我如何做到這一點的任何建議?
編輯: 我可以得到通知顯示,如果我把代碼放在onReceive()showNotification()。 問題似乎是接收器沒有收到警報。可能是因爲我在IntentFilter中沒有正確的操作。
這是我的代碼。我已經從StatusBarNotification插件的PhoneGap構建它,發現here
public class StatusBarNotification extends Plugin {
// Action to execute
public static final String ACTION="notify";
private Context context;
BroadcastReceiver receiver;
public StatusBarNotification() {
this.receiver = null;
}
public void setContext(PhonegapActivity ctx) {
super.setContext(ctx);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(); //Dunno what to put here
if(receiver == null) {
this.receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Notification", "inside onReceive");
/*int icon = R.drawable.notification;
long when = System.currentTimeMillis();
NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, context.getClass());
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification noti = new Notification(icon, "NOTIFICATION", when);
noti.setLatestEventInfo(context, "TITLE", "TEXT", contentIntent);
manager.notify(1, noti);
*/
}
};
ctx.registerReceiver(this.receiver, intentFilter);
}
}
/**
* Executes the request and returns PluginResult
*
* @param action Action to execute
* @param data JSONArray of arguments to the plugin
* @param callbackId The callback id used when calling back into JavaScript
*
* @return A PluginRequest object with a status
* */
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
String ns = Context.NOTIFICATION_SERVICE;
context = this.ctx.getApplicationContext();
PluginResult result = null;
if (ACTION.equals(action)) {
try {
String title = data.getString(0);
String body = data.getString(1);
Log.d("NotificationPlugin", "Notification: " + title + ", " + body);
showNotification(title, body);
result = new PluginResult(Status.OK);
} catch (JSONException jsonEx) {
Log.d("NotificationPlugin", "Got JSON Exception "
+ jsonEx.getMessage());
result = new PluginResult(Status.JSON_EXCEPTION);
}
} else {
result = new PluginResult(Status.INVALID_ACTION);
Log.d("NotificationPlugin", "Invalid action : "+action+" passed");
}
return result;
}
/**
* Displays status bar notification
*
* @param contentTitle Notification title
* @param contentText Notification text
* */
public void showNotification(CharSequence contentTitle, CharSequence contentText) {
Intent intent = new Intent(ctx, ctx.getClass());
PendingIntent pi = PendingIntent.getBroadcast(ctx, 1234, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
AlarmManager am = (AlarmManager) ctx.getSystemService(ctx.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
}
public void onDestroy() {
if (this.receiver != null) {
try {
this.ctx.unregisterReceiver(this.receiver);
} catch (Exception e) {
Log.e("LOG TAG", "Error unregistering network receiver: " + e.getMessage(), e);
}
}
}
}
這是我嘗試過的一件事。可能沒有做正確的壽。我現在有一個「問題」的一件事是我應該向IntentFilter添加什麼樣的動作?這是我在查看BroadcastReceiver示例的時候。 – user713821 2012-02-02 14:59:01
我實際上即將爲應用程序自己解決這個問題。如果我找到解決方案,我會繼續關注此問題並進行更新... – Devgeeks 2012-02-03 01:02:32
鏈接不工作404錯誤。 – 2015-11-21 18:12:05