FORUMS
Remove All Ads from XDA

move service chat head with button

3 posts
Thanks Meter: 0
 
By tsueseres, Junior Member on 19th April 2017, 08:31 PM
Post Reply Email Thread
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

im using MoveHead but i dant make it to work

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();

            }
        });

    }
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;
    }
}
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes