[英]Background service to display toast every 2 min in android
I am writing a small android app that does something in the background(ie service) and I want it to display a toast message after an interval of every 'x' minutes. 我正在编写一个小型的android应用程序,该应用程序在后台(即服务)执行某些操作,并且我希望它在每隔“ x”分钟后显示一条吐司消息。 How do I go about doing it with a broadcast listener and alarmmanager.
如何使用广播侦听器和警报管理器进行操作。 Could somebody please write a sample code to demonstrate it.
有人可以写一个示例代码来演示它。
You can easily do this by using Timer
and TimerTask
in your Service
class. 您可以通过在
Service
类中使用Timer
和TimerTask
轻松地做到这一点。
1. In your Service
class, first create an inner class DisplayToastTimerTask
extending from TimerTask
to display the Toast
message. 1.在
Service
类中,首先创建一个内部类DisplayToastTimerTask
,该类从TimerTask
扩展以显示Toast
消息。 You have to use Handler
with Runnable
to show Toast
from TimerTask
: 您必须使用带有
Runnable
Handler
来显示TimerTask
Toast
:
private class DisplayToastTimerTask extends TimerTask {
Handler mHandler = new Handler();
@Override
public void run() {
// Do something....
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Hello world", Toast.LENGTH_SHORT).show();
}
});
}
}
2. Use Timer
to schedule DisplayToastTimerTask
for repeated execution with an interval of 2 min
2.使用
Timer
安排DisplayToastTimerTask
以2 min
的间隔重复执行
private static final int TIMER_INTERVAL = 120000; // 2 Minute
private static final int TIMER_DELAY = 0;
// Create new Timer
Timer mTimer = new Timer();
mTimer.scheduleAtFixedRate(new DisplayToastTimerTask(), TIMER_DELAY, TIMER_INTERVAL);
EXAMPLE: 例:
#. # Here is the fully working
Service
class:** 这是完全可以使用的
Service
类:**
//MyService.java
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
public class MyService extends Service {
private Timer mTimer;
private Handler mHandler = new Handler();
private static final int TIMER_INTERVAL = 120000; // 2 Minute
private static final int TIMER_DELAY = 0;
@Override
public void onCreate() {
super.onCreate();
if (mTimer != null)
mTimer = null;
// Create new Timer
mTimer = new Timer();
// Required to Schedule DisplayToastTimerTask for repeated execution with an interval of `2 min`
mTimer.scheduleAtFixedRate(new DisplayToastTimerTask(), TIMER_DELAY, TIMER_INTERVAL);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
// Cancel timer
mTimer.cancel();
}
// Required to do some task
// Here I just display a toast message "Hello world"
private class DisplayToastTimerTask extends TimerTask {
@Override
public void run() {
// Do something....
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Hello world", Toast.LENGTH_SHORT).show();
}
});
}
}
}
#. # You can start your
service
like below: 您可以像下面这样启动
service
:
Intent intentMyService = new Intent(context, MyService.class);
mContext.startService(intentMyService);
#. # Don't forget to declare
MyService
class into AndroidManifest.xml
: 不要忘记将
MyService
类声明为AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest>
<application>
<service android:name=".MyService" />
</application>
</manifest>
Hope this will help~ 希望这会有所帮助〜
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.