简体   繁体   English

Android小部件在模拟器中运行良好,但在手机上它变成了Google App小部件

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

I created an Android widget. 我创建了一个Android小部件。
In a Genymotion Nexus 4 emulator running Android 4.4.4 everything works well. 在运行Android 4.4.4的Genymotion Nexus 4模拟器中,一切运行良好。
On my Nexus 4 device running Android 4.4.4 I put the widget on the home screen and it turns into the Google App widget. 在运行Android 4.4.4的Nexus 4设备上,我将小部件放在主屏幕上,然后变成了Google App小部件。
Then it turns into my widget then again into the Google App widget and so on. 然后它变成我的小部件,然后再次进入Google App小部件,依此类推。 It does this until I remove the widget from the home screen. 它执行此操作直到我从主屏幕中删除小部件。
Also I am using parse.com as online storage and on my phone the data doesn't seem to be obtained. 此外,我使用parse.com作为在线存储,在我的手机上似乎没有获得数据。 I can't really tell for sure because the widget keeps changing. 我无法确切地说出因为小部件不断变化。
My widget contains 3 files. 我的小部件包含3个文件。
One that extends the application class: 一个扩展应用程序类:

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();
    }

One WidgetProvider: 一个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;
}

And one broadcastreceiver that handles a button on the widget: 还有一个处理小部件按钮的广播接收器:

@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);


}

Does anyone know what I am doing wrong? 有谁知道我做错了什么?

Edit 1: Adding manifest file as requested 编辑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>

Edit 2, added widget xml file: 编辑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>

Sorry for not answering the last comments but I was really busy. 很抱歉没有回答最后的评论,但我真的很忙。
Thank you to everyone for your help. 感谢大家的帮助。
What happened was that I had return statements in my WidgetProvider's onUpdate, onEnabled, onDisabled and orReceive methods and apparently this is a bad thing. 发生的事情是我在WidgetProvider的onUpdate,onEnabled,onDisabled和orReceive方法中返回了语句,显然这是一件坏事。 Once I removed them this no longer occured. 一旦我删除它们就不再发生了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有Google API的Android应用程序只能在模拟器上使用,而不能在手机上使用 - Android app with Google API works on emulator but not on phone Android - Clickable Widget - 在模拟器上工作,但不在手机上 - Android - Clickable Widget - Working on emulator, but not on phone Android小部件仅在模拟器上有效,而不能在真实设备上有效 - Android widget works only on emulator not on real device Android 模拟器中缺少小部件预览应用程序 - Widget Preview app missing from Android emulator Flutter 应用程序 apk 在手机上无法正常运行,但在模拟器上运行良好 - Flutter app apk does not work properly on phone but works well on emulator Android 将重启手机的应用程序小部件 - 小部件没有执行任何操作? - Android app widget that will reboot phone - widget isn't doing anything? 模拟器中的Android小部件 - Android widget in emulator Android Phone上的iGoogle小工具,作为APP或Widget - iGoogle Gadget on Android Phone as APP or Widget Android应用程序可在模拟器上运行,但无法安装在我的手机上 - Android app works on emulator but will not install on my phone Android phonegap / cordova应用程序可在模拟器中使用,但不能在手机上使用 - Android phonegap/cordova app works in emulator but not on phone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM