0

動作するウィジェットを持つコード ベースがあります。これの基本構造は

MyAppWidget extends AppWidgetProvider
MyWidgetService extends IntentService

MyWidgetServiceアプリMyAppWidgetをライブラリに変えて、同じコードベースから「無料/プロ」コンボを作成することを決定するまで、すべてがうまくいきました。今、私はこのようなものを持っています

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が、呼び出す更新メソッドがわかりません。現状では、プロまたは無料バージョンの代わりにcom.example.myapp.library.MyAppWidget.update()MyWidgetServiceを呼び出します。

フラグを渡し、それがどのウィジェット クラスであるかを伝えることができましたが、これは親クラス (ライブラリMyWidgetService) が更新するプロ/フリー クラスの詳細を知る必要があることを意味します。これは、適切なオブジェクト指向モデルを壊します。

私の質問は、どのウィジェットクラスを更新する必要があるかを伝える適切な方法は何ですか?

これは説明するのが難しいことなので、説明に役立つ場合に備えて、私のクラスのスケルトンの詳細を以下に示します。

どんな助けでも大歓迎です。

ライブラリ 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
    }
}

ライブラリ MyAppWidget を拡張する無料の MyAppWidget

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" />
4

1 に答える 1

1

フラグを渡して、それがどのウィジェット クラスであるかを伝えることができましたが、これは、親クラス (ライブラリ MyWidgetService) が更新するプロ/フリー クラスの詳細を知る必要があることを意味します。これは、適切なオブジェクト指向モデルを壊します。

フラグを渡す代わりに、完全修飾クラス名を渡し、リフレクションを使用してアクセスします。

または、update()のカスタム ブロードキャスト アクションとディスパッチ ロジックによってトリガーされるインスタンス メソッドにしonReceive()ます。次に、サービスに一致するブロードキャストを送信させます。

于 2012-12-09T12:27:58.683 に答える