2012-12-09 1 views
0

나는 작동하는 위젯을 가진 코드 기반을 가지고 있습니다. 이것의 기본 구조는AppWidgetProvider와 IntentService를 Free/Pro 콤보로 올바르게 확장하기

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이 예약 된대로 실행되지만 어떤 업데이트 방법을 호출해야하는지 알 수 없습니다. 그대로 있으니 MyWidgetService은 무료 또는 무료 버전 대신 com.example.myapp.library.MyAppWidget.update()으로 전화 할 것입니다.

플래그를 전달하여 어떤 위젯 클래스인지 알 수 있었지만 부모 클래스 (라이브러리 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 도서관 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

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

답변

1

내가 깃발을 통과하고있는 위젯 클래스를 말할 수 있었다,하지만 그건 내 부모 클래스 (라이브러리 MyWidgetService)이 프로에 대한 세부 정보를 알 필요가 의미/업데이트 할 무료 클래스. 이렇게하면 적절한 객체 지향 모델이 손상됩니다.

플래그를 전달하는 대신 정규화 된 클래스 이름을 전달하고 리플렉션을 사용하여 액세스하십시오.

또는 update()onReceive()에 사용자 지정 브로드 캐스트 작업 및 디스패치 논리에 의해 트리거되는 인스턴스 메서드로 설정하십시오. 그런 다음 서비스에서 일치하는 브로드 캐스트를 보냅니다.

+0

나는 방송 아이디어를 좋아한다. 그래서 당신이 의미하는 바가 무엇인지 명확히 알았습니다. (Java 전문 용어가 많이 있습니다.)'BroadcastReceiver'를 트리거하는 IntentService로부터 Boadcast 메시지를 보내야하고 수신기에서 위젯 디스플레이가 업데이트 되었습니까? – Kirk

+0

@Kirk : 당신의'AppWidgetProvider' *는'BroadcastReceiver'입니다. 'IntentService'에서'AppWidgetProvider'로 브로드 캐스트를 보낼 수 있습니다. – CommonsWare

+0

아, 천재 야! 그렇게 훨씬 깨끗합니다. 고맙습니다. – Kirk

관련 문제