我尝试制作一个应用程序,即使在后台运行,女巫也要监听触摸事件,在这里我在论坛中找到了解决方案,但我无法运行

How can a service listen for touch gestures/events?

如果我理解正确,那么方法是使用正确的windowmanager参数设置一个新视图,这是我的代码!

主要活动:

public class MainActivity extends Activity {


    View mView;
    HUD mHud;
    HUDView mHUDView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mHud = new HUD();
        mHUDView = new HUDView(this);

    }


    @Override
    public boolean onTouchEvent(MotionEvent e) {
        mHud.onCreate(this);
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}


目前,为了进行测试,我只想更改一下视图,

public class HUD extends Service  {

    HUDView mView;

    public void onCreate(Context mContext) {
        super.onCreate();

        mView = new HUDView(this);
                WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.RIGHT | Gravity.TOP;
        params.setTitle("Load Average");
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.addView(mView, params);
    }

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

}



class HUDView extends ViewGroup {

    public HUDView(Context context) {
        super(context);
    }


    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("ontouch", "clicked in the HUD View");
        //Toast.makeText(getContext(),"onTouchEvent", Toast.LENGTH_LONG).show();
        return true;
    }
}


我对Java不是很有信心,我得到的错误是javaNullPointerException错误。我知道发生这种情况是因为对象变为null,但是如何将对象传递给类和子类。

这里的日志:

11-18 11:56:03.791: E/AndroidRuntime(5095): FATAL EXCEPTION: main
11-18 11:56:03.791: E/AndroidRuntime(5095): java.lang.NullPointerException
11-18 11:56:03.791: E/AndroidRuntime(5095):     at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
11-18 11:56:03.791: E/AndroidRuntime(5095):     at android.view.View.<init>(View.java:1810)
11-18 11:56:03.791: E/AndroidRuntime(5095):     at android.view.ViewGroup.<init>(ViewGroup.java:288)
11-18 11:56:03.791: E/AndroidRuntime(5095):     at com.holzi.runinbackground.HUDView.<init>(HUD.java:47)
11-18 11:56:03.791: E/AndroidRuntime(5095):     at com.holzi.runinbackground.HUD.onCreate(HUD.java:22)
11-18 11:56:03.791: E/AndroidRuntime(5095):     at com.holzi.runinbackground.MainActivity.onTouchEvent(MainActivity.java:32)
11-18 11:56:03.791: E/AndroidRuntime(5095):     at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
11-18 11:56:03.791: E/AndroidRuntime(5095):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1685)
11-18 11:56:03.791: E/AndroidRuntime(5095):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1802)
11-18 11:56:03.791: E/AndroidRuntime(5095):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 11:56:03.791: E/AndroidRuntime(5095):     at android.os.Looper.loop(Looper.java:143)
11-18 11:56:03.791: E/AndroidRuntime(5095):     at android.app.ActivityThread.main(ActivityThread.java:4914)
11-18 11:56:03.791: E/AndroidRuntime(5095):     at java.lang.reflect.Method.invokeNative(Native Method)
11-18 11:56:03.791: E/AndroidRuntime(5095):     at java.lang.reflect.Method.invoke(Method.java:521)
11-18 11:56:03.791: E/AndroidRuntime(5095):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)


谢谢!

最佳答案

您正在将this作为上下文传递给HUDViewthis是您的Service,它在附加之前没有基本上下文,因此如果调用getResources()则会抛出该错误。

您将需要等到attachBaseContext被调用,或使用另一个Context实例化HUDView而不是服务(可能是Application上下文)来实例化。

10-06 06:32