不断在后台运行服务。例如,即使应用程序关闭,也必须启动一项服务,该服务将显示一次20秒的敬酒消息。不断在后台运行服务
public class AppService extends IntentService {
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
public AppService() {
super("AppService");
}
@Override
protected void onHandleIntent(Intent workIntent) {
Toast.makeText(getApplicationContext(), "hai", Toast.LENGTH_SHORT).show();
SystemClock.sleep(20000);
}
}
。 – aldok