Move chathead service with button

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Apr 21, 2017 7:09:23 PM via Website

hello, im having trouble moving a chathead with a button, no matter how much i try it always crashes my app
here is the main activity

enter code here

[CODE]package example.com.chatheads;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.sip.SipAudioCall;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.app.Service;

import example.com.chatheads.ChatHeadService.*;

public class MainActivity extends Activity {
private final int xx = 500;
private final int yy = 500;

Button startService,stopService, moveService;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService=(Button)findViewById(R.id.startService);
    stopService=(Button)findViewById(R.id.stopService);
    moveService=(Button)findViewById(R.id.moveService);

    startService.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startService(new Intent(getApplication(), ChatHeadService.class));
            //startService(new Intent(getApplication(), HepUstte.class));

        }
    });
    stopService.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            stopService(new Intent(getApplication(), ChatHeadService.class));

        }
    });

    moveService.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //System.out.println("Hello");

            ChatHeadService  inst = new ChatHeadService();
            inst.UpdateEmployee();

        }
    });

}[/CODE]

and here is the chatheadservice class

[CODE]package example.com.chatheads;

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

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;

public class ChatHeadService extends Service {
public WindowManager windowManager;
public ImageView chatHead;
WindowManager.LayoutParams params;

int xi ;
int yi ;

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

    System.out.println("Hello2");

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    chatHead = new ImageView(this);
    chatHead.setImageResource(R.drawable.face1);

    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;

    chatHead.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          params.x = xi;
          params.y = yi;

          windowManager.updateViewLayout(chatHead, params);
          }

    });
    windowManager.addView(chatHead, params);

}

public void MoveHead(){

    System.out.println("Hello");

    WindowManager.LayoutParams params = (WindowManager.LayoutParams) chatHead.getLayoutParams();

    params.x = 100;
    params.y = 300;

    windowManager.updateViewLayout(chatHead, params);
}

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

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}[/CODE]