当应用程序关闭时发送通知 [英] Send a notification when the app is closed
问题描述
当应用程序完全关闭时,如何以编程方式发送通知?
How is it possible to send a notification programmatically, when the App got completely closed?
示例:用户关闭了应用程序,也在 Android 任务管理器中,然后等待.应用应在 X 秒后或应用检查更新时发送通知.
Example: The User closed the App, also in the Android Taskmanager, and waits. The App should send a notification after X Seconds or when the App check for Updates.
我尝试使用这些代码示例,但是:
I tried to work with these code examples but:
- 在应用关闭时推送通知 - 活动过多/不工作
- 如何我如何让我的应用在关闭时发送通知? - 很多信息,但我不知道如何处理
- 如何在应用程序启动时发送本地通知android关闭? - 很多信息,但我不知道如何处理
- Pushing notifications when app is closed - too many Activities/doesn't work
- How do I get my app to send a notification when it is closed? - much information, but I don't know how to deal with it
- How to send local notification android when app is closed? - much information, but I don't know how to deal with it
如果可以的话,试着用一个例子来解释它,因为初学者(像我一样)可以通过这种方式更容易地学习.
If you can, try to explain it at an example, because beginners (like me) can easier learn it this way.
推荐答案
你可以使用这个服务,你需要做的就是在你的 Activity 生命周期中 onStop() 启动这个服务.使用此代码:startService(new Intent(this, NotificationService.class));
然后您可以创建一个新的 Java 类并将此代码粘贴到其中:
You can use this service all you need to do is Start this service onStop() in your activity lifecycle. With this code:
startService(new Intent(this, NotificationService.class));
then you can create a new Java Class and paste this code in it:
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 结合起来:
After this you only need to combine the service with the 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>
这篇关于当应用程序关闭时发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!