Java 在Free/Pro组合中正确扩展AppWidgetProvider和IntentService

Java 在Free/Pro组合中正确扩展AppWidgetProvider和IntentService,java,android,android-widget,Java,Android,Android Widget,我有一个代码库,它有一个工作的小部件。其基本结构是 MyAppWidget extends AppWidgetProvider MyWidgetService extends IntentService MyWidgetService更新了MyAppWidget,一切都很好,直到我决定将我的应用程序变成一个库,从相同的代码库创建一个“free/pro”组合。现在我有了这样的东西 com.example.myapp.free.MyAppWidget extends com.example.mya

我有一个代码库,它有一个工作的小部件。其基本结构是

MyAppWidget extends AppWidgetProvider
MyWidgetService extends IntentService
MyWidgetService
更新了
MyAppWidget
,一切都很好,直到我决定将我的应用程序变成一个库,从相同的代码库创建一个“free/pro”组合。现在我有了这样的东西

com.example.myapp.free.MyAppWidget extends com.example.myapp.library.MyAppWidget
com.example.myapp.pro.MyAppWidget extends com.example.myapp.library.MyAppWidget
com.example.myapp.library.MyAppWidget extends AppWidgetProvider
com.example.myapp.library.MyWidgetService extends IntentService
MyWidgetService
运行MyAppWidget.update()方法以更新显示

在这种情况下,小部件有一些小的调整,以适应应用程序的版本,所有内容都显示良好,但是
MyWidgetService
不会更新小部件

我已经验证了
MyWidgetService
确实按计划运行,但它不知道调用什么更新方法。目前,
MyWidgetService
将调用com.example.myapp.library.MyAppWidget.update(),而不是pro或免费版本

我可以传递一个标志并告诉它它是哪个widget类,但这意味着我的父类(library
MyWidgetService
)需要知道要更新的pro/free类的详细信息。这会破坏正确的面向对象模型

我的问题是,沟通应该更新哪个小部件类的正确方式是什么

由于这是一件很难解释的事情,下面是我的类的一些基本细节,以防它有助于解释事情

感谢您的帮助

图书馆MyWidgetService

public class MyWidgetService extends IntentService {

    public MyWidgetService() {
        super(MyWidgetService.class.getSimpleName());
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // this calls the update method of the widget
        // but it doesn't know which object to call
        MyAppWidget.update();
        scheduleNextUpdate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);       
        return START_STICKY;
    }

    private void scheduleNextUpdate() {         
        // schedule next time for the service to work
    }   
}
库MyAppWidget

public class MyAppWidget extends AppWidgetProvider {

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        Intent intent = new Intent(context, MyWidgetService.class);
        context.startService(intent);
    }

    public static void update() {
        // update widget display
    }
}
public class MyAppWidget extends com.example.myapp.library.MyAppWidget {

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        // setup free widget
        // what can I do here to pass down to the service
        // to make sure it updates this widget class
    }
}
<!-- use the extended widget from com.example.myapp.free -->
<receiver
    android:name=".MyWidgetApp"
    android:label="@string/clock_widget_name">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>

    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/clock_widget" />
</receiver>
<!-- use the service from the library project -->
<service android:name="com.example.myapp.library.MyWidgetService" />
免费MyAppWidget,扩展库MyAppWidget

public class MyAppWidget extends AppWidgetProvider {

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        Intent intent = new Intent(context, MyWidgetService.class);
        context.startService(intent);
    }

    public static void update() {
        // update widget display
    }
}
public class MyAppWidget extends com.example.myapp.library.MyAppWidget {

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        // setup free widget
        // what can I do here to pass down to the service
        // to make sure it updates this widget class
    }
}
<!-- use the extended widget from com.example.myapp.free -->
<receiver
    android:name=".MyWidgetApp"
    android:label="@string/clock_widget_name">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>

    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/clock_widget" />
</receiver>
<!-- use the service from the library project -->
<service android:name="com.example.myapp.library.MyWidgetService" />
免费应用程序清单

public class MyAppWidget extends AppWidgetProvider {

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        Intent intent = new Intent(context, MyWidgetService.class);
        context.startService(intent);
    }

    public static void update() {
        // update widget display
    }
}
public class MyAppWidget extends com.example.myapp.library.MyAppWidget {

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        // setup free widget
        // what can I do here to pass down to the service
        // to make sure it updates this widget class
    }
}
<!-- use the extended widget from com.example.myapp.free -->
<receiver
    android:name=".MyWidgetApp"
    android:label="@string/clock_widget_name">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>

    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/clock_widget" />
</receiver>
<!-- use the service from the library project -->
<service android:name="com.example.myapp.library.MyWidgetService" />

我能够传递一个标志并告诉它它是哪个小部件类,但这意味着我的父类(库MyWidgetService)需要知道要更新的pro/free类的详细信息。这会破坏正确的面向对象模型

传递完全限定的类名,并使用反射来访问它,而不是传递标志


或者,将
update()
设为实例方法,由
onReceive()
中的自定义广播操作和分派逻辑触发。然后让服务发送匹配的广播。

我喜欢广播的想法。所以我很清楚你对它的意思(里面有很多Java术语),我应该从
IntentService
发送一条boadcast消息,触发
BroadcastReceiver
,从接收者那里,小部件显示被更新了?@Kirk:Your
AppWidgetProvider
BroadcastReceiver
。我是说你可以从你的
IntentService
向你的
AppWidgetProvider
发送广播。啊,天才!那样会更干净。非常感谢。