Android:未调用警报或服务来更新小部件

时间:2011-03-20 04:54:32

标签: android service widget alarm

我创建了一个小部件来显示每5-10秒刷新一次的当前日期。 将来会增加持续时间。在onUpdate方法中创建一个警报以启动服务,然后使用日期更新小部件。 当我调用小部件时没有任何反应。 不知道问题是警报还是服务。 粘贴下面的代码。请告诉我可能出错的地方..

谢谢, 萨姆

main.xml中

      <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      >
  <TextView  
      android:id="@+id/scores"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/hello"
     android:paddingTop="35dip" android:paddingLeft="29dip" android:textColor="#000000"/>
  </LinearLayout>

窗口小部件提供程序xml

  <?xml version="1.0" encoding="utf-8"?>
  <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
     android:minWidth="146dip"
     android:minHeight="72dip"
     android:updatePeriodMillis="1000"
     android:initialLayout="@layout/main"
  />      

Android Manifest xml

       <?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
         package="com.helloandroid.worldcupscores"
         android:versionCode="1"
         android:versionName="1.0">
       <application android:icon="@drawable/icon" android:label="@string/app_name">
           <!-- Broadcast Receiver that will process AppWidget updates -->
           <receiver android:name=".WorldCupScores" android:label="@string/widget_name">
               <intent-filter>
                   <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
               </intent-filter>
           <meta-data android:name="android.appwidget.provider" android:resource="@xml/scores_widget_provider" />
           </receiver>

           <!-- Service to perform web API queries -->        
           <service android:name="WorldCupScores$UpdateService" />
       </application>
       <uses-permission android:name="android.permission.INTERNET" />
       <uses-sdk android:minSdkVersion="7" />
   </manifest>             

WorldCupScores.java

      package com.helloandroid.worldcupscores;

  import android.app.Activity;
  import android.os.Bundle;

  import java.util.Date;

  import android.app.PendingIntent;
  import android.app.Service;
  import android.appwidget.AppWidgetManager;
  import android.appwidget.AppWidgetProvider;
  import android.content.ComponentName;
  import android.content.Context;
  import android.content.Intent;
  import android.content.res.Resources;
  import android.net.Uri;
  import android.os.IBinder;
  import android.text.format.Time;
  import android.util.Log;
  import android.widget.RemoteViews;
  import android.app.Activity;
  import android.app.AlarmManager;
  import java.util.Calendar;
  import java.util.GregorianCalendar;
  import android.widget.Toast;

  public class WorldCupScores extends AppWidgetProvider {
      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager,
          int[] appWidgetIds) {
          AlarmManager alarmManager;
          Intent intent = new Intent(context, UpdateService.class);
          PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
          alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
          Calendar cal = Calendar.getInstance();
           cal.setTimeInMillis(System.currentTimeMillis());
          cal.add(Calendar.SECOND, 10);
           alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5*1000, pendingIntent);       
      }

      public static class UpdateService extends Service {
          @Override
          public void onStart(Intent intent, int startId) {

            // Build the widget update for today
            RemoteViews updateViews = new RemoteViews(this.getPackageName(),
                    R.layout.main);

            Date date = new Date();

            updateViews.setTextViewText(R.id.scores, "Current Time "
                    + date);

              // Push update for this widget to the home screen
              ComponentName thisWidget = new ComponentName(this, WorldCupScores.class);
              AppWidgetManager manager = AppWidgetManager.getInstance(this);
              manager.updateAppWidget(thisWidget, updateViews);
          }

          @Override
          public IBinder onBind(Intent intent) {
              // We don't need to bind to this service
              return null;
          }
      }
  }                   

1 个答案:

答案 0 :(得分:0)

更改

PendingIntent.FLAG_UPDATE_CURRENT

intent.FLAG_ACTIVITY_NEW_TASK