android小部件无法正常工作

时间:2014-04-04 10:27:55

标签: xml android-widget

    package com.appointext.widget;

    import com.bmsce.appointext.R;
//import com.appointext.frontend.*;

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

public class Widget extends AppWidgetProvider {

    //DateFormat df = new SimpleDateFormat("hh:mm:ss");

 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
   final int N = appWidgetIds.length;

   // Perform this loop procedure for each App Widget that belongs to this
   // provider
   for (int i = 0; i < N; i++) {
     int appWidgetId = appWidgetIds[i];

     // Create an Intent to launch ExampleActivity
     Intent intent = new Intent(context, com.appointext.frontend.AppoinTextActivity.class);
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

     // Get the layout for the App Widget and attach an on-click listener
     // to the button
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
     views.setOnClickPendingIntent(R.id.button, pendingIntent);

     // To update a label
     //views.setTextViewText(R.id.widget1label, "AppoinText");

     // Tell the AppWidgetManager to perform an update on the current app
     // widget
     appWidgetManager.updateAppWidget(appWidgetId, views);
   }
 }
}

这是我的主要java类,我试图调用前端的appointextactivity类。主屏幕中的小部件说&#34;无法加载小部件&#34;。有人请告诉我我的错误是什么。

1 个答案:

答案 0 :(得分:0)

添加小部件时不会调用onUpdate(), 您需要覆盖onUpdate()方法

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

同时确保您的清单如下

 <receiver android:name="com.example.app.Widget" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_info" />
 </receiver>
相关问题