简体   繁体   English

在服务内重复一个动作

[英]Repeat an action inside of a service

  1. From MainActivity I'm passing an intent to startService.从 MainActivity,我将意图传递给 startService。 That service is successfully started.该服务已成功启动。

  2. 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”,只会看到日期和时间字符串)。

  3. 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.

相关问题 Android:在LinearLayout(性能)中重复一个复杂的视图 - Android: Repeat a complex view inside a LinearLayout (Performance) 工具栏中的浮动操作按钮 - Floating Action button trapped inside toolbar 片段按钮内的动作侦听器不起作用 - Action listener inside a fragment button not working 在服务中显示Snackbar - Showing a Snackbar from inside a Service 如何在ActionBar中显示动作的图标和标题? - How to display both icon and title of action inside ActionBar? 如何只允许在“浮动操作按钮”的圆圈内单击? - How to allow clicks inside Floating Action Button's circle only? 如何在后台服务中放置具有无限foor循环的线程 - How to place a thread with and infinite foor loop inside a background service 如何将浮动操作按钮连接到 Android 项目中的活动 - How do I connect my floating action button to an activity inside my Android project Android:检查屏幕上是否有其他操作按钮(主页,后退等)? - Android : check wether action button (home,back, etc.. ) is inside the screen or not? 如何为dimens.xml 中的浮动操作按钮声明两个不同的用户定义大小? - How can i declare two different userdefined size for Floating Action Button inside dimens.xml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM