Android小部件在模拟器上运行良好,但在手机上却变成了Google应用程序小部件。

16

我创建了一个 Android 小部件。
在运行 Android 4.4.4 的 Genymotion Nexus 4 模拟器上,一切都工作正常。
但是在我运行 Android 4.4.4 的 Nexus 4 设备上,我把小部件放在主屏幕上,它就变成了 Google 应用程序的小部件。
然后它又变回我的小部件,再次变成 Google 应用程序的小部件,如此反复,直到我从主屏幕上删除该小部件为止。
此外,我正在使用 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,添加了小部件的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>

@DavidWasser 现在已经添加了它。 - Para
请也发布the_widget_info.xml文件。 - David Wasser
@DavidWasser 也加了那个。 - Para
请在模拟器上运行正常但在您的设备上无法运行时,请提供屏幕截图。谢谢(只是好奇症状是什么样的)。 - Yenchi
你有收到任何异常吗?特别是在你的update/layout方法中? - JohanShogun
1个回答

0

非常抱歉之前没有回复评论,我实在是太忙了。
感谢大家的帮助。
问题出在我的WidgetProvider的onUpdate、onEnabled、onDisabled和orReceive方法中有返回语句,显然这是不好的。一旦我删除了它们,问题就不再出现。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接