AlarmManager带的PhoneGap [英] AlarmManager with phonegap

查看:199
本文介绍了AlarmManager带的PhoneGap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用StatusBarNotification插件(安卓)的PhoneGap的解雇通知。现在,我想在一个特定的时间,这一点,从我读过,我必须使用Android的AlarmManager。我已经尝试了一些办法,但似乎无法得到它的工作。

I'm using the StatusBarNotification plugin (Android) for Phonegap to fire a notification. Now I want to to this at a specific time, and from what I've read I have to use Android's AlarmManager. I've tried some approaches, but can't seem to get it to work.

我如何能做到这一点有什么建议?

Any suggestions on how I can do this?

编辑: 我可以通知显示,如果我把code中的onReceive()来showNotification()。 这个问题似乎是,接收器没有接收到报警,啄。可能是因为我没有在IntentFilter的正确的行动。

I can get the notification to show if I put the code in onReceive() to showNotification(). The problem seems to be that the receiver doesn't receive the Alarm-thingy. Probably because I don't have the right action in the IntentFilter.

这是我的code。我从一StatusBarNotification插件的PhoneGap构建它,找到<一href="https://github.com/phonegap/phonegap-plugins/tree/master/Android/StatusBarNotification">here

this is my code. I've build it from the StatusBarNotification plugin for Phonegap, found 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);
        }
    }
}

}

推荐答案

您应该使用LocalNotification.js设定警报的具体日期和时间。因为我已经使用它,它工作正常。

You should use LocalNotification.js to set alert for specific date and time. Because I had used it and it works fine.

这篇关于AlarmManager带的PhoneGap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆