【问题标题】:How can I share code between my application and my service?如何在我的应用程序和我的服务之间共享代码?
【发布时间】:2018-01-07 18:35:16
【问题描述】:

为了这个问题,我用 cmets 替换了一些逻辑并简化了layous。但是问题的主要目的应该是相同的。

所以我有一个带有按钮和文本视图的活动视图。

layout_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click me"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

代码隐藏 MainActivity.java

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // retrieve data from internet and display in the textview
            findViewById(R.id.textView).setText(response);
        }
    });

现在有了this library,我可以在活动外的浮动窗口中显示带有服务layout_main。请参阅 github 页面上的演示 gif。通过我的 xml 上的另一个按钮,我启动了服务。

startService(new Intent(MainActivity.this, MyService.class));

Demo code 来自 github 页面:

MyService.java

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

    windowManagerContainer = new WindowManagerContainer(this);
    chatHeadManager = new DefaultChatHeadManager<String>(this, windowManagerContainer);
    chatHeadManager.setViewAdapter(new ChatHeadViewAdapter<String>() {

        @Override
        public View attachView(String key, ChatHead chatHead, ViewGroup parent) {
            View cachedView = viewCache.get(key);
            if (cachedView == null) {
                LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                View view = inflater.inflate(R.layout.layout_main, parent, false);
                // 'problem' here
                cachedView = view;
                viewCache.put(key, view);
            }
            parent.addView(cachedView);
            return cachedView;
        }
...

但现在的问题是,我必须在服务中创建重复的代码来处理网络请求并显示文本。和activity中的代码基本一致。

所以我的问题是,我能否以某种方式共享“代码隐藏”以用于我的活动和服务,还是必须从我的活动中创建代码的副本?

【问题讨论】:

  • 嗯,您可以创建另一个 Java 类,将代码放在那里,然后从您的活动和服务中使用该 Java 类的实例。
  • 例如,将侦听器附加到按钮,仍然需要在两个类中都这样做吗?
  • 您可以将代表您的膨胀布局的Linearlayout 传递给处理这两种情况的一些常见Java 类。给你的LinearLayout一个id,这样你就可以在活动中setContentView()之后获得它。
  • 听起来是一个可行的解决方案,稍后会尝试并在此处发布结果

标签: android android-activity android-service


【解决方案1】:

我确实可以将我的视图作为一个类的参数来处理两个视图

对于我的活动:

View view = getLayoutInflater().inflate(R.layout_main, null);
setContentView(view);
new ViewHandler(MainActivity.this, view);
...

为我服务:

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_main, false);
new ViewHandler(MyService.this, view);

ViewHandler:

public ViewHandler(Context context, View view) {
    super(context, view);
    // get ids from the view and attach handlers or do other things with it
    Button button = (Button) view.findViewById(R.id.button);
    ....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多