Android 如何使Intentservice重复作业?

Android 如何使Intentservice重复作业?,android,Android,我有一个intentservice,当在myDataBsae中找到值时通知我,但若数据为null,则不应通知我,所以 如何使我的Intentservice每50秒检查一次我数据库的内容(我使用AlarmManager,但我的服务每50秒通知我一次,尽管我的数据库=null,) 当我触发intentservice时,我希望它单独工作 我的服务是: public class ShowTimeServer extends IntentService { NotificationManager

我有一个intentservice,当在myDataBsae中找到值时通知我,但若数据为null,则不应通知我,所以

  • 如何使我的Intentservice每50秒检查一次我数据库的内容(我使用AlarmManager,但我的服务每50秒通知我一次,尽管我的数据库=null,)
  • 当我触发intentservice时,我希望它单独工作
  • 我的服务是:

        public class ShowTimeServer extends IntentService  {
    NotificationManager nm;
    static final int uniqueID=326534;
    public ShowTimeServer() {
        super("ShowTimeServer");
        // TODO Auto-generated constructor stub
    }
    
    /* (non-Javadoc)
     * @see android.app.IntentService#onHandleIntent(android.content.Intent)
     */
    @Override
    protected void onHandleIntent(Intent arg0) {
        // TODO Auto-generated method stub
    
        ShowTimeDB RetrieverDB =new ShowTimeDB(this);
        RetrieverDB.open();
        String data = RetrieverDB.getShowNotify();
        RetrieverDB.close();
    
    
    
        if (!(data.equals(null))){
    
            nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    
            Intent intent=new Intent(this,TodayShow.class);
            PendingIntent pi= PendingIntent.getActivity(this, 0, intent, 0  );
            CharSequence x=(CharSequence) data;
            Notification n=new Notification(R.drawable.ic_launcher,"YOU HAVE SHOW" ,System.currentTimeMillis());
            n.setLatestEventInfo(this, "ShowTime", x, pi);
            n.defaults=Notification.DEFAULT_ALL;
    
            nm.notify(uniqueID,n);
            //pi.cancel();
            //nm.cancel(uniqueID);
    
        }
    }
    
    }
    
  • intentService不应该每X秒做一次事情。为此,还有很多其他的解决方案。如果您坚持使用服务完成此任务,您可以将其设置为普通的前台/后台服务(取决于用户离开应用程序时您是否希望这样做),然后在那里执行

  • intentService已在其他线程上运行。这就是为什么可以对onHandleIntent方法执行长时间操作的原因


  • 你的意思是我不能在itI中设置runnable或timer吗?我不明白你指的是什么。请进一步解释。我的意思是我可以使用下面的sach代码并将我的通知代码放入其中***>private TimerTask mTask=new TimerTask(){@Override public void run(){//任何你想要的postDelayed(这个,重复间隔);//清洗并重复…};