从其他活动更新布局视图

时间:2014-08-06 15:54:23

标签: android android-layout

我有这个服务类

public TouchService(Context ctx, View v, WindowManager wm) {
    ctx = mContext;
    v = touchLayout;
    wm = mWindowManager;
}

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

    prefs = PreferenceManager
            .getDefaultSharedPreferences(TouchService.this);
    int index = prefs.getInt("position choise", 0);
    int triggerWidth = prefs.getInt("width choise", 14);
    int triggerHeight = prefs.getInt("height choise", 300);
    int triggerPosLeft = prefs.getInt("posLeft choise", 300);
    int triggerPosRight = prefs.getInt("posRight choise", 300);

    touchLayout = new LinearLayout(TouchService.this);

    int triggerColor = prefs.getInt("trigger color",
            (getResources().getColor(android.R.color.transparent)));
    touchLayout.setBackgroundColor(triggerColor);
    touchLayout.setOnTouchListener(this);
    vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);


    mParams = new WindowManager.LayoutParams(
            triggerWidth, // width of layout
            triggerHeight, // height of layout
            WindowManager.LayoutParams.TYPE_PHONE, // Type Phone, These are non-application windows providing user interaction with the phone (in particular incoming calls).
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, // this window won't ever get key input focus
            PixelFormat.TRANSLUCENT);

    switch (index) {
        case 0:
            mParams.gravity = Gravity.LEFT | Gravity.TOP;
            mParams.y = triggerPosLeft;

            break;

        case 1:
            mParams.gravity = Gravity.RIGHT | Gravity.TOP;
            mParams.y = triggerPosRight;
            break;


    }

    Log.i(TAG, "add View");
    mWindowManager.addView(touchLayout, mParams);
}

我想将活动类中的视图更新为**更改其背景颜色而不重新启动服务。 **

以下是控制活动的相关代码:

public boolean onOptionsItemSelected(MenuItem item) {
    editor.putInt("trigger color",
            (getResources().getColor(android.R.color.transparent)));
    editor.commit();
    tservice = new TouchService(mContext, touchLayout, wm);


    this.finish();
    return true;
}

我在服务类上创建了构造函数,在控件活动中创建了TouchService的新实例,但我不知道如何访问视图来更改背景颜色。我做了一些尝试,但我总是得到nullPointerException或找不到资源ID。 我如何获得这种布局?

0 个答案:

没有答案