2

我正在关注这个http://www.vogella.com/articles/AndroidWidgets/article.html教程。

这是我的 Widetprovider:

import java.util.Random;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

import com.asco.countdown.R;

public class MyWidgetProvider extends AppWidgetProvider {

  private static final String ACTION_CLICK = "ACTION_CLICK";

  @Override
  public void onEnabled(Context context) {
      Log.d("WID", "enabled");
  }



  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
      int[] appWidgetIds) {

      Log.d("WID", "update");

    // Get all ids
    ComponentName thisWidget = new ComponentName(context,
        MyWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
    for (int widgetId : allWidgetIds) {
      // Create some random data
      int number = (new Random().nextInt(100));

      RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
          R.layout.widget);
      Log.w("WidgetExample", String.valueOf(number));
      // Set the text
      remoteViews.setTextViewText(R.id.update, String.valueOf(number));

      // Register an onClickListener
      Intent intent = new Intent(context, MyWidgetProvider.class);

      intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

      PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
          0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
      appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
  }
} 

这是我的 widgetinfo.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider 
      xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/widget"
    android:minHeight="72dp"
    android:minWidth="300dp"
    android:updatePeriodMillis="5000000" >

</appwidget-provider> 

这是我的清单:

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <receiver android:name="MyWidgetProvider" >
        <intent-filter>
                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
                <action android:name="android.appwidget.action.APPWIDGET_DELETED" />
                <action android:name="android.appwidget.action.APPWIDGET_DISABLED" />  
        </intent-filter>

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

显示小部件,但既没有调用 onEnabled() 也没有调用 onUpdate() 方法,onClickListener 也不起作用。我尝试在清单中提供 AppWidgetProvider 的完整路径,将 AppWidgetProvider 放在另一个包中(现在它在默认包中)等等,但没有任何反应,现在我没有想法了。有人可以帮我吗?

4

2 回答 2

1

尝试在 maifest 中给出全名

<receiver android:name="MyWidgetProvider" >

<receiver android:name="com.asco.countdown.MyWidgetProvider" > 

或者什么全名

于 2012-08-24T12:25:46.340 回答
0

好的,在 4.0.3 模拟器和 2.3 设备上进行了测试,都运行良好。我的自定义 4.1 JB rom 似乎有问题。

于 2012-08-24T15:23:45.830 回答