在android系统中每3秒显示通知 [英] show notification in every 3 sec in android

查看:347
本文介绍了在android系统中每3秒显示通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建我的应用程序,每3秒造成在通知中的一项服务。我创建这个code,但它的工作只有一个,当我启动我的应用程序的时间。我想在每3秒获得通知!甚至当我关闭我的应用程序,我得到通知! (因为这个我创建的服务)
请帮助我。

I want create a service within my app that every 3 sec creates a notification. i create this code but it works only one time when i launch my app. i want get notification in every 3 sec!! even when i close my app i get notifications!! ( because of this i create service) please help me.

public class notifService extends Service {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private static final int HELLO_ID = 1;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    final Intent intent1 = new Intent(this, notifService.class);

    scheduler.schedule(new Runnable() {
        @Override
        public void run() {
            // Look up the notification manager server
            NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            // Create your notification
            int icon = R.drawable.fifi;
            CharSequence tickerText = "Hello";
            long when = System.currentTimeMillis();
            Notification notification = new Notification(icon, tickerText,when);
            Context context = getApplicationContext();
            CharSequence contentTitle = "My notification";
            CharSequence contentText = "Hello World!";
            PendingIntent pIntent = PendingIntent.getActivity(notifService.this, 0, intent1, 0);
            notification.setLatestEventInfo(context, contentTitle,contentText, pIntent);
            // Send the notification
            nm.notify(HELLO_ID, notification);
        }
    }, 3, SECONDS);
}

@Override
public void onDestroy() {
    super.onDestroy();
}

}

推荐答案

进度你使用的方法的执行一次性行动的。

The schedule method you're using executes a one-shot action.

您需要使用<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html#scheduleWithFixedDelay%28java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit%29\"相对=nofollow> ScheduledExecutorService.scheduleWithFixedDelay

You need to use ScheduledExecutorService.scheduleWithFixedDelay:

创建并执行在首次启用的定期操作
  给定的初始延迟后,并且随后用给定的延迟
  一个执行的终止和的开始之间
  下一个。

Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.

试试这个:

scheduler.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
          // your code
        }
    }, 3, 3, SECONDS);

请注意额外的 3 在方法调用,因为这方法预计四个参数。

Note the extra 3 in the method call as this methods expects four arguments.

这篇关于在android系统中每3秒显示通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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