IT TIP

동일한 앱에 여러 위젯을 추가하는 방법은 무엇입니까?

itqueen 2020. 11. 26. 20:33
반응형

동일한 앱에 여러 위젯을 추가하는 방법은 무엇입니까?


Android 위젯을 마쳤습니다. 이제 사용자가 선택할 수 있도록이 위젯의 ​​다양한 크기가 필요합니다.

예를 들어 중소형 위젯이 필요하므로 사용자가 앱을 설치하고 홈 화면을 누른 상태에서 위젯을 선택하면 위젯 메뉴에서 앱 이름은 같지만 크기가 같은 위젯 3 개가 표시되기를 원합니다. . 이 같은:

helloSmall helloMedium helloLarge

중간 크기를 준비했지만 동일한 앱에서 작은 크기와 큰 크기를 어떻게 추가 할 수 있습니까? 세 가지 크기 모두에 동일한 데이터와 작업이 포함되어 있다는 것을 알면 크기와 배경 만 다릅니다.


다음과 같이 매니페스트 파일의 각 유형에 대한 수신자 정의가 필요합니다.

    <receiver android:name=".MyWidget" android:label="@string/medium_widget_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/medium_widget_provider" />
    </receiver>

    <receiver android:name=".MyWidget" android:label="@string/large_widget_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/large_widget_provider" />
    </receiver>

이렇게하면 AppWidgetProvider여러 위젯에 동일한 클래스를 사용할 수 있으며 위젯 이름과 크기는 <appwidget-provider>XML에 정의되어 있습니다.

이제 <appwidget-provider>XML 에있는 것보다 위젯에 더 많은 차이점이 필요한 경우 서로 다른 유형 간의 모든 공통 동작을 구현하는 기본 위젯 클래스를 만듭니다.

public abstract class MyBaseWidget extends AppWidgetProvider

그런 다음 각 구체적인 구현은 MyBaseWidget을 확장 할 수 있습니다. 그런 다음 매니페스트 파일에 다음과 같은 구체적인 구현 각각에 대한 수신자 정의가 있습니다.

    <receiver android:name=".MyMediumWidget" android:label="@string/medium_widget_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/medium_widget_provider" />
    </receiver>

    <receiver android:name=".MyLargeWidget" android:label="@string/large_widget_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/large_widget_provider" />
    </receiver>

실제로 각 위젯의 android : name은 달라야합니다. 예와 같이이 작업을 수행하면 위젯 목록에 하나의 위젯 만 표시됩니다.


여러분, 저도 같은 문제가있었습니다.

실제로 두 번째 위젯 공급자도 추가해야합니다.

<receiver android:name=**".MyWidget**" android:label="@string/medium_widget_name">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
        android:resource="@xml/medium_widget_provider" />
</receiver>

<receiver android:name=**".MyWidget2"** android:label="@string/large_widget_name">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
        android:resource="@xml/large_widget_provider" />
</receiver>

즐겨


좋아, 기본적으로 다음이 필요합니다.

layout file fore each widget. ex: main_small.xml, main_medium.xml ...

in the xml directory add a provider for each widget. ex: small_provider.xml, medium_provider.xml ... and so on (note if you don't have an xml directory add it under the drawable directory).

now what!

  • define a receiver in the manifest for each widget. (just like the example in the main answer)

  • you can use the same layout or deferent layout. basically this is up to you.

  • in your provider you should have something like this:

<?xml version="1.0" encoding="utf-8"?>

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="146dip"
    android:minHeight="138dip"
    android:updatePeriodMillis="10000"
    android:initialLayout="@layout/main"
    />

make sure, for each provider to specify the target layout file you want to use. in this code I'm asking for the file main.xml in the layout directory. for my medium widget for example i'll have another provider with the same exact code but i'll change the last line

> android:initialLayout="@layout/medium".

I hope this helps if not let me know and I can upload a working example on my website and you can take a closer look at it. please let me know how it goes.

best of luck.


Some extra info to the other answers...

  • If you are duplicating the files mentioned, and if your widget uses a Service to provide some functionality, you might have to duplicate your service.

  • If you duplicate your Service, remember to update your manifest with the new service, otherwise the new service won't run...

This wasted some time for me.


If you use any BroadcastReceiver to send Intents to your duplicate Services... don't forget to update that code too:

  • you must now send intents to each of the services.

참고URL : https://stackoverflow.com/questions/2570004/how-to-add-multiple-widgets-in-the-same-app

반응형