第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如果最近發(fā)送過通知,如何防止再次發(fā)送?

如果最近發(fā)送過通知,如何防止再次發(fā)送?

慕哥9229398 2023-07-13 16:57:57
我正在制作一個(gè)應(yīng)用程序,它將 JSON 文檔作為輸入并以用戶友好的方式顯示信息。如果某些信息超出某些參數(shù),該應(yīng)用程序還會(huì)發(fā)送推送通知。我遇到的問題是信息需要非常最新,這意味著應(yīng)用程序每 10 秒就會(huì)收到一個(gè)新的 JSON。這使得應(yīng)用程序每 10 秒發(fā)送一次推送通知,這太頻繁了。有沒有辦法讓我指定一個(gè)休息時(shí)間,如果應(yīng)用程序最近發(fā)送了通知,則不會(huì)發(fā)送通知?或者我可以這樣做,如果用戶沒有清除通知,它就不會(huì)發(fā)送新通知?總的來說,我對編程還比較陌生,對 Android-Studio 也很陌生。我在Android開發(fā)者頁面上查看了NotificationManager,看看那里是否有東西,但我找不到任何東西。    if variable1") < variable1MinValue || variable1 > variable1MaxValue||     variable2 < variable2MinValue|| variable2 > variable2MaxValue){                                                NotificationManager notif=     (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);    Notification notify=new Notification.Builder    (getApplicationContext()).setContentTitle("ERROR: value     Error").setContentText("Please see app for more information.").    setSmallIcon(R.drawable.error_notif).setSound(soundUri).build();    notify.flags |= Notification.FLAG_AUTO_CANCEL;    notif.notify(0, notify);我正在為我的業(yè)務(wù)制作這個(gè)應(yīng)用程序,所以我不能在程序中留下任何公司特定的內(nèi)容。如果有什么需要我澄清的,請告訴我!我希望能夠讓它最快每小時(shí)只發(fā)送幾次通知。理想情況下,也許每 30 分鐘到一小時(shí)一次。
查看完整描述

1 回答

?
汪汪一只貓

TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊

如果您使用的是桌面設(shè)備,您可以查看 Google Guava,它有許多緩存實(shí)用程序,包括創(chuàng)建具有逐出時(shí)間的條目的功能。使用它,您可以添加條目并驅(qū)逐 10 分鐘。然后,當(dāng)新的 JSON 進(jìn)來時(shí),您可以檢查它是否存在于緩存中。如果不是,則發(fā)送通知,如果是,則重新設(shè)置驅(qū)逐時(shí)間。


您也可以編寫自己的 EvictionMap。擴(kuò)展ConcurrentHashMap,并在構(gòu)造函數(shù)中創(chuàng)建一個(gè)線程并啟動(dòng)它。在線程內(nèi)部,您可以檢查 X 秒(聽起來像是每 5 秒一次)并逐出條目。地圖需要 <User, long>,其中 long 是驅(qū)逐時(shí)間。您可以創(chuàng)建自己的 put() 和 get() 以及可能的 touch() ,這會(huì)將驅(qū)逐時(shí)間重置為 System.getCurrentMillis();


(我剛剛找到了幾年前使用過的版本。它可以在管理線程的方式上進(jìn)行一些改進(jìn))


import java.util.Iterator;

import java.util.Set;

import java.util.concurrent.ConcurrentHashMap;


public class EvictionList<K>


{


private final ConcurrentHashMap<K, Long> evictionList = new ConcurrentHashMap<K, Long>();

private long evictionTime;

private final EvictionThread t;

public EvictionList(int evictionTimeInSeconds)

{

    this.evictionTime = evictionTimeInSeconds * 1000;

    t = new EvictionThread(this, evictionTime);

    Thread thread = new Thread(t);

    thread.start();

}


public void touch(K o)

{

    evictionList.put(o, System.currentTimeMillis());

}


public void evict()

{

    long current = System.currentTimeMillis();

    for (Iterator<K> i=evictionList.keySet().iterator(); i.hasNext();)

    {

        K k = i.next();

        if (current > (evictionList.get(k) + evictionTime) )

        {

            i.remove();

        }

    }

}


public void setEvictionTime(int timeInSeconds)

{

    evictionTime = timeInSeconds * 1000;

    t.setEvictionTime(evictionTime);

}


public Set<K> getKeys()

{

    return evictionList.keySet();

}


public void stop()

{

    t.shutDown();

}


@Override

protected void finalize()

{

    t.shutDown();   

}


private class EvictionThread implements Runnable

{

    private volatile long evictionTime;

    private EvictionList list;

    private volatile boolean shouldRun = true;


    private EvictionThread(EvictionList list, long evictionTime)

    {

        this.list = list;

        this.evictionTime = evictionTime;

    }


    public void shutDown()

    {

        shouldRun = false;

    }


    public void setEvictionTime(long time)

    {

        evictionTime = time;

    }


    public void run()

    {

        while (shouldRun)

        {

            try

            {

                Thread.sleep(evictionTime);

            }

            catch (Exception ex) {}

            list.evict();

        }

    }

}

}


查看完整回答
反對 回復(fù) 2023-07-13
  • 1 回答
  • 0 關(guān)注
  • 159 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)