在android浮动视图中动态更改文本

时间:2015-03-12 22:50:27

标签: android android-layout android-intent android-activity android-service

我只是Android的初学者,我被分配为我们的android团队项目创建浮动视图,我已经能够创建服务到目前为止,现在我试图以某种方式将浮动视图中的图像更改为一个动态变化的文本但不知道我该怎么做,所以如果有人能帮助我,我会非常感激。

FloatService.Java

package com.example.justeen.floatexample;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

public class FloatService extends Service{

private WindowManager windowManager;
private ImageView floatIcon;

@Override public IBinder onBind(Intent intent) {
    // Not used
    return null;
}

@Override public void onCreate() {
    super.onCreate();

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    floatIcon = new ImageView(this);
    floatIcon.setImageResource(R.drawable.android_head);

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 100;

    windowManager.addView(floatIcon, params);

    try {
        floatIcon.setOnTouchListener(new View.OnTouchListener() {
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        initialX = params.x;
                        initialY = params.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();
                        return true;
                    case MotionEvent.ACTION_UP:
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        params.x = initialX + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY + (int) (event.getRawY() - initialTouchY);
                        windowManager.updateViewLayout(floatIcon, params);
                        return true;
                }
                return false;
            }
        });
    } catch (Exception e) {
        // TODO: handle exception
    }
}
@Override
public void onDestroy() {
    super.onDestroy();
    if (floatIcon != null) windowManager.removeView(floatIcon);
}

}

这是更新代码

package com.example.justeen.floatexample;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

public class FloatService extends Service{

private WindowManager windowManager;
private TextView textView;
private Bundle extras;

@Override public IBinder onBind(Intent intent) {
    // Not used
    return null;
}

@Override public void onCreate() {
    super.onCreate();

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    textView = new TextView(this);

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 100;

    windowManager.addView(textView, params);

    try {
        textView.setOnTouchListener(new View.OnTouchListener() {
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        initialX = params.x;
                        initialY = params.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();
                        return true;
                    case MotionEvent.ACTION_UP:
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        params.x = initialX + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY + (int) (event.getRawY() - initialTouchY);
                        windowManager.updateViewLayout(textView, params);
                        return true;
                }
                return false;
            }
        });
    } catch (Exception e) {
        // TODO: handle exception
    }
}

@Override
public int onStartCommand( Intent intent , int flags , int startId ) {
    super.onStartCommand(intent, flags, startId);

    extras = intent.getExtras();


    return START_REDELIVER_INTENT;
}

    @Override
public void onDestroy() {
    super.onDestroy();
    if (textView != null) windowManager.removeView(textView);
}

}

1 个答案:

答案 0 :(得分:0)

1。而不是ImageView,请在您的服务中使用TextView

2。我猜你正在使用startservice(Intent)来拨打你的服务,因为你没有使用onBind()。现在,每次您需要更新"浮动文本" ,在您的活动中调用startservice方法,但在Intent中添加字符串Extra以包含要更新的文本。示例(在您的活动中):

  Intent i = new Intent(context,YourService.class);
  i.putExtra("TEXT","Updated the text in my service");
  startService(i);

3。在您的服务中实施onStartCommand()。每次使用上一步中提到的startservice时,都会调用此方法。 onCreate只被调用一次。

4. 在方法onStartCommand中检索intent并使用额外的String更新服务中的TextView。示例(在onStartCommand()内):

  String textToUpdate = intent.getStringExtra("TEXT");
  yourTextView.setText(textToUpdate);

请注意,从Service延伸并使用startService会使您的责任在stopservicestopSelf时不再需要时停止服务。