android浮动视图泡泡

时间:2016-11-21 21:11:14

标签: android service

我正在使用android studio为Android构建一个应用程序,每当我直接在手机上运行应用程序时,当我点击按钮时启动FloatingViewService时崩溃。我必须在手机上运行它,因为我的笔记本电脑上没有虚拟化。

是的,我确实添加了许可:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

我还在AndroidManifest.xml文件中声明了FloatingViewService:

<service android:name=".FloatingViewService" />

但仍然没有运气。我做错了什么?

这是我的整个代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.icetea09.demofloatingview.MainActivity">

<Button
    android:id="@+id/btn_show_floating_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Show Floating View"/>

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;
import android.widget.Toast;

public class FloatingViewService extends Service {

private WindowManager mWindowManager;
private ImageView mImgFloatingView;
private boolean mIsFloatingViewAttached = false;

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if(!mIsFloatingViewAttached){
        mWindowManager.addView(mImgFloatingView, mImgFloatingView.getLayoutParams());
    }
    return super.onStartCommand(intent, flags, startId);
}

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

    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    mImgFloatingView = new ImageView(this);
    mImgFloatingView.setImageResource(R.mipmap.ic_launcher);

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

    params.gravity = Gravity.TOP | Gravity.LEFT;

    mWindowManager.addView(mImgFloatingView, params);

    mIsFloatingViewAttached = true;

    mImgFloatingView.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);
                    mWindowManager.updateViewLayout(mImgFloatingView, params);
                    return true;
            }
            return false;
        }
    });
}

public void removeView() {
    if (mImgFloatingView != null){
        mWindowManager.removeView(mImgFloatingView);
        mIsFloatingViewAttached = false;
    }
}

@Override
public void onDestroy() {
    Toast.makeText(getApplicationContext(), "onDestroy", Toast.LENGTH_SHORT);
    super.onDestroy();
    removeView();
}

}

public class MainActivity extends ActionBarActivity implements View.OnClickListener{

private Button mBtnShowView;
private boolean mIsFloatingViewShow; //Flag variable used to identify if the Floating View is visible or not

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mBtnShowView = (Button)findViewById(R.id.btn_show_floating_view);
    mBtnShowView.setOnClickListener(this);
    mIsFloatingViewShow = false;
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btn_show_floating_view:
            if(mIsFloatingViewShow){
                hideFloatingView();
                mIsFloatingViewShow = false;
                mBtnShowView.setText(R.string.show_floating_view);
            }
            else{
                showFloatingView();
                mIsFloatingViewShow = true;
                mBtnShowView.setText(R.string.hide_floating_view);
            }
            break;
    }
}

private void showFloatingView() {
    startService(new Intent(getApplicationContext(), FloatingViewService.class));
}

private void hideFloatingView() {
    stopService(new Intent(getApplicationContext(), FloatingViewService.class));
}

}

1 个答案:

答案 0 :(得分:0)

使用

WindowManager.LayoutParams.TYPE_TOAST

代替

WindowManager.LayoutParams.TYPE_PHONE