M00*_*007 14 android phonegap-plugins cordova
我在状态栏通知中有一个问题,间隔为10秒.我已经完成了代码,通过创建插件一次显示它.但我想每隔10分钟显示一次.所以我用它每10分钟AlarmManager生成一次通知但是它没有调用类的onReceive(Context ctx, Intent intent)方法FirstQuoteAlarm.我有以下代码用于显示通知和AlarmManager.
public void showNotification( CharSequence contentTitle, CharSequence contentText ) {
int icon = R.drawable.nofication;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, contentTitle, when);
Intent notificationIntent = new Intent(ctx, ctx.getClass());
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1, notification);
Date dt = new Date();
Date newdate = new Date(dt.getYear(), dt.getMonth(), dt.getDate(),10,14,dt.getSeconds());
long triggerAtTime = newdate.getTime();
long repeat_alarm_every = 1000;
QuotesSetting.ON = 1;
AlarmManager am = ( AlarmManager ) ctx.getSystemService(Context.ALARM_SERVICE );
//Intent intent = new Intent( "REFRESH_ALARM" );
Intent intent1 = new Intent(ctx,FirstQuoteAlarm.class);
PendingIntent pi = PendingIntent.getBroadcast(ctx, 0, intent1, 0 );
am.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, repeat_alarm_every, pi);
Log.i("call2","msg");
}
Run Code Online (Sandbox Code Playgroud)
使用 ScheduledExecutorService。这通常会带来更好的结果。
它的目的是每隔几分钟在后台重复一次操作。从延迟开始等等。查看:http ://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html
下面是一个类,其中的方法设置 ScheduledExecutorService 在一个小时内每十秒发出一次蜂鸣声:
import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep");
};
final ScheduledFuture beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}
}}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1106 次 |
| 最近记录: |