[英]Repeat an action inside of a service
From MainActivity I'm passing an intent to startService.从 MainActivity,我将意图传递给 startService。 That service is successfully started.
该服务已成功启动。
On that service, there is a small rectangle of text ("hello, world") that is originally initialized in an XML layout.在该服务上,有一个小矩形文本(“hello, world”),最初在 XML 布局中初始化。 In the service code reproduced below, I'm first changing that textView01 text to instead be a string that expresses the date and time.
在下面复制的服务代码中,我首先将 textView01 文本更改为表示日期和时间的字符串。 That too is successful (ie, the user never sees "hello, world" onscreen as the service is started, only the date and time string).
这也是成功的(即,当服务启动时,用户永远不会在屏幕上看到“hello, world”,只会看到日期和时间字符串)。
In the same code immediately below that, I update that date and time delayed by 3 seconds.在其正下方的同一代码中,我更新了延迟 3 秒的日期和时间。 Successful (I'm look at the string change in real time onscreen).
成功(我在屏幕上实时查看字符串变化)。
My problem: I can't get it to keep repeating that update.我的问题:我无法让它继续重复该更新。 I've tried every loop and delay and wait and sleep and everything else that I've researched and can think of, all to no avail.
我已经尝试了每一个循环、延迟、等待和睡眠以及我研究过和能想到的所有其他方法,但都无济于事。 It currently just stops after having updated--once--after 3 seconds.
它目前只是在 3 秒后更新一次后停止。
Thank you for any help that you can provide.感谢您提供的任何帮助。
package com.example.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.graphics.PixelFormat;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.os.Handler;
public class FloatingWidgetShowService extends Service{
WindowManager windowManager;
View floatingView, collapsedView, expandedView;
WindowManager.LayoutParams params ;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
floatingView = LayoutInflater.from(this).inflate(R.layout.floating_widget_layout, null);
TextView innerView = floatingView.findViewById(R.id.textView01);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_TOAST,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
windowManager.addView(floatingView, params);
expandedView = floatingView.findViewById(R.id.Layout_Expended);
collapsedView = floatingView.findViewById(R.id.Layout_Collapsed);
floatingView.findViewById(R.id.Widget_Close_Icon).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopSelf();
}
});
String currentDateandTime = sdf.format(new Date());
innerView.setText(currentDateandTime);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
String currentDateandTime = sdf.format(new Date());
innerView.setText(currentDateandTime);
}
}, 3000);
expandedView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
collapsedView.setVisibility(View.VISIBLE);
expandedView.setVisibility(View.GONE);
}
});
floatingView.findViewById(R.id.MainParentRelativeLayout).setOnTouchListener(new View.OnTouchListener() {
int X_Axis, Y_Axis;
float TouchX, TouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
X_Axis = params.x;
Y_Axis = params.y;
TouchX = event.getRawX();
TouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
collapsedView.setVisibility(View.GONE);
expandedView.setVisibility(View.VISIBLE);
return true;
case MotionEvent.ACTION_MOVE:
params.x = X_Axis + (int) (event.getRawX() - TouchX);
params.y = Y_Axis + (int) (event.getRawY() - TouchY);
windowManager.updateViewLayout(floatingView, params);
return true;
}
return false;
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
if (floatingView != null) windowManager.removeView(floatingView);
}
}
Just repost your update to the Handler
, like this:只需将您的更新重新发布到
Handler
,如下所示:
handler.postDelayed(new Runnable() {
@Override
public void run() {
String currentDateandTime = sdf.format(new Date());
innerView.setText(currentDateandTime);
// Now post the update to run again in 3 seconds
handler.postDelayed(this, 3000);
}
}, 3000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.