在服务内重复一个动作
Repeat an action inside of a service
我从 MainActivity 向 startService 传递一个意图。该服务已成功启动。
在该服务上,有一个最初在 XML 布局中初始化的小矩形文本(“hello, world”)。在下面复制的服务代码中,我首先将 textView01 文本更改为表示日期和时间的字符串。这也是成功的(即,当服务启动时,用户永远不会在屏幕上看到“hello, world”,只有日期和时间字符串)。
在其正下方的相同代码中,我更新了延迟 3 秒的日期和时间。成功(我在屏幕上实时查看字符串变化)。
我的问题:我无法继续重复更新。我已经尝试了每一个循环、延迟、等待和睡眠以及我研究过和能想到的所有其他方法,但都无济于事。它目前只是在更新后停止--一次--3 秒后。
感谢您提供的任何帮助。
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);
}
}
只需将您的更新重新发布到 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);
我从 MainActivity 向 startService 传递一个意图。该服务已成功启动。
在该服务上,有一个最初在 XML 布局中初始化的小矩形文本(“hello, world”)。在下面复制的服务代码中,我首先将 textView01 文本更改为表示日期和时间的字符串。这也是成功的(即,当服务启动时,用户永远不会在屏幕上看到“hello, world”,只有日期和时间字符串)。
在其正下方的相同代码中,我更新了延迟 3 秒的日期和时间。成功(我在屏幕上实时查看字符串变化)。
我的问题:我无法继续重复更新。我已经尝试了每一个循环、延迟、等待和睡眠以及我研究过和能想到的所有其他方法,但都无济于事。它目前只是在更新后停止--一次--3 秒后。
感谢您提供的任何帮助。
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);
}
}
只需将您的更新重新发布到 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);