簡體   English   中英

Android小部件在模擬器中運行良好,但在手機上它變成了Google App小部件

[英]Android widget works well in emulator but on phone it turns into the Google App widget

我創建了一個Android小部件。
在運行Android 4.4.4的Genymotion Nexus 4模擬器中,一切運行良好。
在運行Android 4.4.4的Nexus 4設備上,我將小部件放在主屏幕上,然后變成了Google App小部件。
然后它變成我的小部件,然后再次進入Google App小部件,依此類推。 它執行此操作直到我從主屏幕中刪除小部件。
此外,我使用parse.com作為在線存儲,在我的手機上似乎沒有獲得數據。 我無法確切地說出因為小部件不斷變化。
我的小部件包含3個文件。
一個擴展應用程序類:

public class MyApp extends Application
{
    private MyModel model;

     @Override
    public void onCreate() {
        Parse.enableLocalDatastore(this);
        ParseObject.registerSubclass(GdbdData.class);
        Parse.initialize(this, "-", "-");

        if(model == null) {
            Intent intent = new Intent(this,GdbdWidgetProvider.class);
            intent.setAction(WidgetUtils.WIDGET_REFRESH_STORAGE);
            intent.putExtra("userId",123);
            sendBroadcast(intent);
        }

        super.onCreate();
    }

一個WidgetProvider:

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    final String action = intent.getAction();
    if (action.equals(WidgetUtils.WIDGET_REFRESH_STORAGE))
    {
        MyApp app = ((MyApp)context.getApplicationContext());
        int userId = intent.getIntExtra("userId",0);
        TryGetModelFromRemoteStorage(userId,context);
    }

}


static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId)
{
    MyApp app = ((MyApp)context.getApplicationContext());
    MyModel model = app.getModel();

    // Construct the RemoteViews object
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.the_widget);

    PendingIntent clickerPendingIntent = buildButtonPendingIntent(context);

    // I change some textviews and imageviews inside the widget here

    appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}

public static PendingIntent buildButtonPendingIntent(Context context) {

    // initiate widget update request
    Intent intent = new Intent();
    intent.setAction(WidgetUtils.WIDGET_UPDATE_ACTION);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    return pendingIntent;
}

還有一個處理小部件按鈕的廣播接收器:

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(WidgetUtils.WIDGET_UPDATE_ACTION)) {
        updateWidget(context);
    }
}

private void updateWidget(Context context) {


    MyApp app = ((MyApp)context.getApplicationContext());
    MyModel model = app.getModel();

    //I update some fields in the model based on some business rules at this point

    MyWidgetProvider.SendUpdateMessageToWidgets(context);


}

有誰知道我做錯了什么?

編輯1:根據請求添加清單文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.app.main" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:name="MyApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name=".GdbdWidgetProvider" >
            <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/the_widget_info" />
        </receiver>
        <receiver
            android:name=".TapMarkDayIntentReceiver"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.my.app.intents.UPDATE_WIDGET" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/the_widget_info" />
        </receiver>
    </application>

</manifest>

編輯2,添加了widget xml文件:

  <?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="140dp" android:minHeight="140dp"
    android:updatePeriodMillis="1000000"
    android:previewImage="@drawable/example_appwidget_preview"
    android:initialLayout="@layout/the_widget" android:widgetCategory="home_screen"
    android:initialKeyguardLayout="@layout/the_widget"></appwidget-provider>

很抱歉沒有回答最后的評論,但我真的很忙。
感謝大家的幫助。
發生的事情是我在WidgetProvider的onUpdate,onEnabled,onDisabled和orReceive方法中返回了語句,顯然這是一件壞事。 一旦我刪除它們就不再發生了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM