Android没有获得服务上的getApplicationContext

时间:2017-02-28 08:11:38

标签: java android ontouchlistener ontouchevent ontouch

当我使用触控听众时。在触摸监听器内部,我调用了一种方法。方法参数需要活动和视图。当我在我的服务中使用getApplicationContext()时,它会显示出来 Wrong 1st argument type. Found: 'android.content.Context', required: 'android.app.Activity'

我的代码如下:

MyService.java

chatHead.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();
                startX = event.getX();
                startY = event.getY();
                return false;
            case MotionEvent.ACTION_UP:
                float endX = event.getX();
                float endY = event.getY();
                if (isAClick(startX, endX, startY, endY)) {
                    Log.d("LockView", "clicked2");
                    CircularAnim.fullActivity(getApplicationContext(), v)
                            .colorOrImageRes(R.color.color1)
                            .go(new CircularAnim.OnAnimationEndListener() {
                                @Override
                                public void onAnimationEnd() {
                                    startActivity(new Intent(getApplicationContext(), BrowserActivity.class));
                                }
                            });
                }
                return true;
            case MotionEvent.ACTION_MOVE:
                params.x = initialX
                        + (int) (event.getRawX() - initialTouchX);
                params.y = initialY
                        + (int) (event.getRawY() - initialTouchY);
                windowManager.updateViewLayout(chatHead, params);
                return false;
        }
        return true;
    }
});
windowManager.addView(chatHead, params);

private boolean isAClick(float startX, float endX, float startY, float endY) {
    float differenceX = Math.abs(startX - endX);
    float differenceY = Math.abs(startY - endY);
    if (differenceX > CLICK_ACTION_THRESHHOLD/* =5 */ || differenceY > CLICK_ACTION_THRESHHOLD) {
        return false;
    }
    return true;
}

CircularAnim.java

public static FullActivityBuilder fullActivity(Activity activity, View triggerView) {
return new FullActivityBuilder(activity, triggerView);
}
public static void init(long perfectMills, long fullActivityPerfectMills, int colorOrImageRes) {
    sPerfectMills = perfectMills;
    sFullActivityPerfectMills = fullActivityPerfectMills;
    sColorOrImageRes = colorOrImageRes;
}

getApplicationContext()进入CircularAnim.fullActivity(MainActivity.this, view),但不在CircularAnim.fullActivity

如何解决getApplicationContext

CircularAnim.fullActivity(getApplicationContext(), v)的问题

请帮帮我。

CircularAnim的完整代码

public class CircularAnim {
public static final long PERFECT_MILLS = 618;
public static final int MINI_RADIUS = 0;
private static Long sPerfectMills;
private static Long sFullActivityPerfectMills;
private static Integer sColorOrImageRes;
private static long getPerfectMills() {
    if (sPerfectMills != null)
        return sPerfectMills;
    else
        return PERFECT_MILLS;
}
private static long getFullActivityMills() {
    if (sFullActivityPerfectMills != null)
        return sFullActivityPerfectMills;
    else
        return PERFECT_MILLS;
}
private static int getColorOrImageRes() {
    if (sColorOrImageRes != null)
        return sColorOrImageRes;
    else
        return android.R.color.white;
}
public interface OnAnimationEndListener {
    void onAnimationEnd();
}
@SuppressLint("NewApi")
public static class VisibleBuilder {
    private View mAnimView, mTriggerView;
    private Float mStartRadius, mEndRadius;
    private long mDurationMills = getPerfectMills();
    private boolean isShow;
    private OnAnimationEndListener mOnAnimationEndListener;
    public VisibleBuilder(View animView, boolean isShow) {
        mAnimView = animView;
        this.isShow = isShow;
        if (isShow) {
            mStartRadius = MINI_RADIUS + 0F;
        } else {
            mEndRadius = MINI_RADIUS + 0F;
        }
    }
    public VisibleBuilder triggerView(View triggerView) {
        mTriggerView = triggerView;
        return this;
    }
    public VisibleBuilder startRadius(float startRadius) {
        mStartRadius = startRadius;
        return this;
    }
    public VisibleBuilder endRadius(float endRadius) {
        mEndRadius = endRadius;
        return this;
    }
    public VisibleBuilder duration(long durationMills) {
        mDurationMills = durationMills;
        return this;
    }
    @Deprecated //You can use method - go(OnAnimationEndListener onAnimationEndListener).
    public VisibleBuilder onAnimationEndListener(OnAnimationEndListener onAnimationEndListener) {
        mOnAnimationEndListener = onAnimationEndListener;
        return this;
    }
    public void go() {
        go(null);
    }
    public void go(OnAnimationEndListener onAnimationEndListener) {
        mOnAnimationEndListener = onAnimationEndListener;
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
            doOnEnd();
            return;
        }
        int rippleCX, rippleCY, maxRadius;
        if (mTriggerView != null) {
            int[] tvLocation = new int[2];
            mTriggerView.getLocationInWindow(tvLocation);
            final int tvCX = tvLocation[0] + mTriggerView.getWidth() / 2;
            final int tvCY = tvLocation[1] + mTriggerView.getHeight() / 2;
            int[] avLocation = new int[2];
            mAnimView.getLocationInWindow(avLocation);
            final int avLX = avLocation[0];
            final int avTY = avLocation[1];
            int triggerX = Math.max(avLX, tvCX);
            triggerX = Math.min(triggerX, avLX + mAnimView.getWidth());
            int triggerY = Math.max(avTY, tvCY);
            triggerY = Math.min(triggerY, avTY + mAnimView.getHeight());
            int avW = mAnimView.getWidth();
            int avH = mAnimView.getHeight();
            rippleCX = triggerX - avLX;
            rippleCY = triggerY - avTY;
            int maxW = Math.max(rippleCX, avW - rippleCX);
            int maxH = Math.max(rippleCY, avH - rippleCY);
            maxRadius = (int) Math.sqrt(maxW * maxW + maxH * maxH) + 1;
        } else {
            rippleCX = (mAnimView.getLeft() + mAnimView.getRight()) / 2;
            rippleCY = (mAnimView.getTop() + mAnimView.getBottom()) / 2;
            int w = mAnimView.getWidth();
            int h = mAnimView.getHeight();
            maxRadius = (int) Math.sqrt(w * w + h * h) + 1;
        }
        if (isShow && mEndRadius == null)
            mEndRadius = maxRadius + 0F;
        else if (!isShow && mStartRadius == null)
            mStartRadius = maxRadius + 0F;
        try {
            Animator anim = ViewAnimationUtils.createCircularReveal(
                    mAnimView, rippleCX, rippleCY, mStartRadius, mEndRadius);
            mAnimView.setVisibility(View.VISIBLE);
            anim.setDuration(mDurationMills);
            anim.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    doOnEnd();
                }
            });
            anim.start();
        } catch (Exception e) {
            e.printStackTrace();
            doOnEnd();
        }
    }
    private void doOnEnd() {
        if (isShow)
            mAnimView.setVisibility(View.VISIBLE);
        else
            mAnimView.setVisibility(View.INVISIBLE);
        if (mOnAnimationEndListener != null)
            mOnAnimationEndListener.onAnimationEnd();
    }
}
@SuppressLint("NewApi")
public static class FullActivityBuilder {
    private Activity mActivity;
    private View mTriggerView;
    private float mStartRadius = MINI_RADIUS;
    private int mColorOrImageRes = getColorOrImageRes();
    private Long mDurationMills;
    private OnAnimationEndListener mOnAnimationEndListener;
    private int mEnterAnim = android.R.anim.fade_in, mExitAnim = android.R.anim.fade_out;
    public FullActivityBuilder(Activity activity, View triggerView) {
        mActivity = activity;
        mTriggerView = triggerView;
    }
    public FullActivityBuilder startRadius(float startRadius) {
        mStartRadius = startRadius;
        return this;
    }

    public FullActivityBuilder colorOrImageRes(int colorOrImageRes) {
        mColorOrImageRes = colorOrImageRes;
        return this;
    }
    public FullActivityBuilder duration(long durationMills) {
        mDurationMills = durationMills;
        return this;
    }
    public FullActivityBuilder overridePendingTransition(int enterAnim, int exitAnim) {
        mEnterAnim = enterAnim;
        mExitAnim = exitAnim;
        return this;
    }
    public void go(OnAnimationEndListener onAnimationEndListener) {
        mOnAnimationEndListener = onAnimationEndListener;
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
            doOnEnd();
            return;
        }
        int[] location = new int[2];
        mTriggerView.getLocationInWindow(location);
        final int cx = location[0] + mTriggerView.getWidth() / 2;
        final int cy = location[1] + mTriggerView.getHeight() / 2;
        final ImageView view = new ImageView(mActivity);
        view.setScaleType(ImageView.ScaleType.CENTER_CROP);
        view.setImageResource(mColorOrImageRes);
        final ViewGroup decorView = (ViewGroup) mActivity.getWindow().getDecorView();
        int w = decorView.getWidth();
        int h = decorView.getHeight();
        decorView.addView(view, w, h);
        int maxW = Math.max(cx, w - cx);
        int maxH = Math.max(cy, h - cy);
        final int finalRadius = (int) Math.sqrt(maxW * maxW + maxH * maxH) + 1;
        try {
            Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, mStartRadius, finalRadius);
            int maxRadius = (int) Math.sqrt(w * w + h * h) + 1;
            if (mDurationMills == null) {
                double rate = 1d * finalRadius / maxRadius;
                mDurationMills = (long) (getFullActivityMills() * Math.sqrt(rate));
            }
            final long finalDuration = mDurationMills;
            anim.setDuration((long) (finalDuration * 0.9));
            anim.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    doOnEnd();
                    mActivity.overridePendingTransition(mEnterAnim, mExitAnim);
                    mTriggerView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (mActivity.isFinishing()) return;
                            try {
                                Animator anim = ViewAnimationUtils.createCircularReveal(view, cx, cy,
                                        finalRadius, mStartRadius);
                                anim.setDuration(finalDuration);
                                anim.addListener(new AnimatorListenerAdapter() {
                                    @Override
                                    public void onAnimationEnd(Animator animation) {
                                        super.onAnimationEnd(animation);
                                        try {
                                            decorView.removeView(view);
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                                anim.start();
                            } catch (Exception e) {
                                e.printStackTrace();
                                try {
                                    decorView.removeView(view);
                                } catch (Exception e1) {
                                    e1.printStackTrace();
                                }
                            }
                        }
                    }, 1000);
                }
            });
            anim.start();
        } catch (Exception e) {
            e.printStackTrace();
            doOnEnd();
        }
    }
    private void doOnEnd() {
        mOnAnimationEndListener.onAnimationEnd();
    }
}
public static VisibleBuilder show(View animView) {
    return new VisibleBuilder(animView, true);
}
public static VisibleBuilder hide(View animView) {
    return new VisibleBuilder(animView, false);
}
public static FullActivityBuilder fullActivity(Activity activity, View triggerView) {
    return new FullActivityBuilder(activity, triggerView);
}
public static void init(long perfectMills, long fullActivityPerfectMills, int colorOrImageRes) {
    sPerfectMills = perfectMills;
    sFullActivityPerfectMills = fullActivityPerfectMills;
    sColorOrImageRes = colorOrImageRes;
}

}

3 个答案:

答案 0 :(得分:1)

这样做......

  1. 公共类BaseService扩展了服务{

        public Context mContext;
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            mContext = getApplicationContext();
        } }
    

答案 1 :(得分:0)

getApplicationContext()与应用程序相关,而不是与活动相关,因此您应该使用activityname.this或getBaseContext(),并且您尝试使用Application Context执行UI Thread操作,使用Activity Context执行与Activity UI相关的操作,而不是应用程序上下文

答案 2 :(得分:0)

如果您需要从服务访问活动的上下文,则可以尝试在服务的构造函数中传递Activity上下文。例如:

活动

public class BT_Client extends Activity{

     private BtService mBTService;

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ...

        mBtService = new BtService(this, ...);
     }
}

服务

public class BtService {

    Context myActivityContext;

    public BtService(Context context, ...) {

           myActivityContext = context;

           ...
    }
}