Android的小窗口更新

Android的小窗口更新

本文介绍了Android的小窗口更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时间。 Java的方式工作,但服务的方式没有。

Based on this tutorial i created a widget that should show the time. The java way works, but the service way doesn't.

HelloWidget.java:

HelloWidget.java:

public class HelloWidget extends AppWidgetProvider {

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

    Intent intent = new Intent(context, UpdateService.class);
    context.startService(intent);
}

UpdateService.java:

UpdateService.java:

public final class UpdateService extends Service {
@Override
public void onStart(Intent intent, int startId) {
    RemoteViews updateViews = new RemoteViews(this.getPackageName(), R.layout.main);

    Date date = new Date();
    java.text.DateFormat format = SimpleDateFormat.getTimeInstance(
            SimpleDateFormat.MEDIUM, Locale.getDefault());
            updateViews.setTextViewText(R.id.widget_textview, "Current Time " + format.format(date));

    ComponentName thisWidget = new ComponentName(this, HelloWidget.class);
    AppWidgetManager manager = AppWidgetManager.getInstance(this);
    manager.updateAppWidget(thisWidget, updateViews);
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}

该hello_widget_provider.xml有该行:机器人:updatePeriodMillis =1000

The hello_widget_provider.xml has this row: android:updatePeriodMillis="1000"

问题:小部件显示当前的时间,但它没有更新(Java的方式是)

Problem: The widget shows the current time but it's not updating (the java-way is).

我的主要目标是更新部件在如每天18:56。IDK如果这是很好的方式,但我试图修改的OnUpdate方法如下图所示,但它与ALARM_SERVICE一个问题:ALARM_SERVICE不能被解析为一个变量

My main goal is to update the widget at e.g. 18:56 every day.Idk if this is the good way, but i tried to modify the onUpdate method like below, but it has a problem with ALARM_SERVICE: ALARM_SERVICE cannot be resolved to a variable

public class HelloWidget extends AppWidgetProvider {

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

    AlarmManager alarmManager;
    Intent intent = new Intent(context, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
      intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 18);
    cal.set(Calendar.MINUTE, 56);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 86400, pendingIntent);

}}

任何帮助是AP preciated。

Any help is appreciated.

推荐答案

回首的时候,我问这个问题使我意识到我在过去的1,5年来有多少教训。

Looking back the time I asked this question makes me realize how much I learned in the past 1,5 years.

由于我刚才一直在与一个部件,在过去两个星期,我刚刚发现我的这个悬而未决的问题,我决定来解决它。我希望它可以帮助别人。

Since I have just been working with a widget in the last two weeks and I have just found this unanswered question of mine I decided to solve it. I hope it helps others.

在AppWidgetProvider应该是这样的:

The AppWidgetProvider should look like this:

public class MyWidget extends AppWidgetProvider {

    public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
    public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
    private PendingIntent service = null;

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


           final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

           final Calendar TIME = Calendar.getInstance();
           TIME.set(Calendar.MINUTE, 0);
           TIME.set(Calendar.SECOND, 0);
           TIME.set(Calendar.MILLISECOND, 0);

           final Intent i = new Intent(context, MyService.class);

           if (service == null)
           {
               service = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
           }

           m.setRepeating(AlarmManager.RTC, TIME.getTime().getTime(), 1000 * 1, service);

    }

      @Override
       public void onDeleted(Context context, int[] appWidgetIds) {
               super.onDeleted(context, appWidgetIds);
       }

       @Override
       public void onDisabled(Context context) {
               super.onDisabled(context);

               final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
               if (service != null)
               {
                   m.cancel(service);
               }
       }

       @Override
       public void onEnabled(Context context) {
               super.onEnabled(context);
       }

       @Override
       public void onReceive(Context context, Intent intent) {

         super.onReceive(context, intent);

       }

}

该服务:

public class MyService extends Service
{
    @Override
    public void onCreate()
    {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        buildUpdate();

        return super.onStartCommand(intent, flags, startId);
    }

    private void buildUpdate()
    {
        String lastUpdated = DateFormat.format("hh:mm:ss", new Date()).toString();

        RemoteViews view = new RemoteViews(getPackageName(), R.layout.widget_layout);
        view.setTextViewText(R.id.btn_widget, lastUpdated);

        ComponentName thisWidget = new ComponentName(this, MyWidget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, view);
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
}

此外,注册商,并在清单中的服务:

Also, register the provider and the Service in the Manifest:

<receiver android:name="MyWidget" android:label="Tutorial_Widget 1x1">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="com.something.MyWidget.ACTION_WIDGET_RECEIVER"/>
            </intent-filter>
            <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info"/>
        </receiver>

         <service android:enabled="true" android:name=".MyService" />

这篇关于Android的小窗口更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 07:03