应用程序关闭时发送通知

2024-01-09

当应用程序完全关闭时,如何以编程方式发送通知?

示例:用户关闭了应用程序(也在 Android 任务管理器中),然后等待。应用程序应在 X 秒后或当应用程序检查更新时发送通知。

我尝试使用这些代码示例,但是:

  • 应用程序关闭时推送通知 https://stackoverflow.com/questions/37906371/pushing-notifications-when-app-is-closed- 活动太多/不起作用
  • 如何让我的应用程序在关闭时发送通知? https://stackoverflow.com/questions/24002188/how-do-i-get-my-app-to-send-a-notification-when-it-is-closed- 很多信息,但我不知道如何处理它
  • 如何在应用程序关闭时发送本地通知android? https://stackoverflow.com/questions/24327453/how-to-send-local-notification-android-when-app-is-closed- 很多信息,但我不知道如何处理它

如果可以的话,尝试用一个例子来解释它,因为初学者(像我)可以通过这种方式更容易地学习它。


您只需在 Activity 生命周期中的 onStop() 中启动此服务即可使用此服务。有了这个代码:startService(new Intent(this, NotificationService.class));然后你可以创建一个新的 Java 类并将以下代码粘贴到其中:

public class NotificationService extends Service {

    Timer timer;
    TimerTask timerTask;
    String TAG = "Timers";
    int Your_X_SECS = 5;


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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand");
        super.onStartCommand(intent, flags, startId);

        startTimer();

        return START_STICKY;
    }


    @Override
    public void onCreate() {
        Log.e(TAG, "onCreate");


    }

    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy");
        stoptimertask();
        super.onDestroy();


    }

    //we are going to use a handler to be able to run in our TimerTask
    final Handler handler = new Handler();


    public void startTimer() {
        //set a new Timer
        timer = new Timer();

        //initialize the TimerTask's job
        initializeTimerTask();

        //schedule the timer, after the first 5000ms the TimerTask will run every 10000ms
        timer.schedule(timerTask, 5000, Your_X_SECS * 1000); //
        //timer.schedule(timerTask, 5000,1000); //
    }

    public void stoptimertask() {
        //stop the timer, if it's not already null
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    public void initializeTimerTask() {

        timerTask = new TimerTask() {
            public void run() {

                //use a handler to run a toast that shows the current timestamp
                handler.post(new Runnable() {
                    public void run() {

                        //TODO CALL NOTIFICATION FUNC
                        YOURNOTIFICATIONFUNCTION();

                    }
                });
            }
        };
    }
}

之后,您只需将服务与manifest.xml结合起来:

<service
            android:name=".NotificationService"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="your.app.domain.NotificationService" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

应用程序关闭时发送通知 的相关文章

随机推荐