之后×时间的Andr​​oid窗口小部件更新 [英] Android widget update after x time

查看:140
本文介绍了之后×时间的Andr​​oid窗口小部件更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学的Andr​​oid,我想写一个控件,它正在更新每秒计数器,而是介于它缺少something.I'm获得1在屏幕上。我使用的是安卓4.0模拟器。

 包com.ibluekey;

进口java.text.DateFormat中;
进口java.text.SimpleDateFormat的;
进口java.util.Date;
进口java.util.Locale中;
进口com.ibluekey.R;

进口android.app.PendingIntent;
进口android.app.Service;
进口android.appwidget.AppWidgetManager;
进口android.appwidget.AppWidgetProvider;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Handler;
进口android.os.IBinder;
进口android.os.SystemClock;
进口android.util.Log;
进口android.widget.RemoteViews;
进口android.widget.Toast;

公共类WidgetActivity扩展AppWidgetProvider {
    公共静态WidgetActivity Widgetke = NULL;
    公共静态上下文的背景下;
    公共静态AppWidgetManager appWidgetManager;
    公共静态INT appWidgetIds [];
    私人处理程序mHandler =新的处理程序();
    私有静态诠释计数= 0;
    @覆盖
    公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
        如果(空==上下文)语境= WidgetActivity.context;
        如果(空== appWidgetManager)appWidgetManager = WidgetActivity.appWidgetManager;
        如果(空== appWidgetIds)appWidgetIds = WidgetActivity.appWidgetIds;

        WidgetActivity.Widgetke =这一点;
        WidgetActivity.context =背景;
        WidgetActivity.appWidgetManager = appWidgetManager;
        WidgetActivity.appWidgetIds = appWidgetIds;

        Log.d(PXR,的onupdate);

        最终诠释N = appWidgetIds.length;
        的for(int i = 0; I&n种;我++){
            INT appWidgetId = appWidgetIds [I]

            updateAppWidget(背景下,appWidgetManager,appWidgetId);
        }

    }


    静态无效updateAppWidget(上下文的背景下,AppWidgetManager appWidgetManager,
            INT appWidgetId){

        算上++;

        / *意向意图=新的意图(背景下,UpdateService.class);
        PendingIntent pendingIntent = PendingIntent.getService(上下文,0,意图,0);

        RemoteViews remoteViews =新RemoteViews(context.getPackageName(),R.layout.widget);
        remoteViews.setOnClickPendingIntent(R.id.mainlayout,pendingIntent);

        remoteViews.setTextViewText(R.id.test_out,+计);

        //告诉小插件管理器
        appWidgetManager.updateAppWidget(appWidgetId,remoteViews); * /

        RemoteViews意见=新RemoteViews(context.getPackageName(),R.layout.widget);
        views.setTextViewText(R.id.test_out,算上+);
        appWidgetManager.updateAppWidget(appWidgetId,意见);
    }



    公共静态类UpdateService延伸服务{
        @覆盖
        公共无效ONSTART(意向意图,诠释startId){
            //WidgetActivity.Widgetke.onUpdate(context,appWidgetManager,appWidgetIds);
            //Toast.makeText(context,更新小工具,Toast.LENGTH_SHORT).show();

            WidgetActivity.Widgetke.mHandler.removeCallbacks(WidgetActivity.Widgetke.mUpdateTimeTask);
            WidgetActivity.Widgetke.mHandler.postDelayed(WidgetActivity.Widgetke.mUpdateTimeTask,100);
        }

        @覆盖
        公众的IBinder onBind(意向为arg0){
            返回null;
        }
    }

    私人可运行mUpdateTimeTask =新的Runnable(){
           公共无效的run(){

               WidgetActivity.Widgetke.onUpdate(背景下,appWidgetManager,appWidgetIds);
               WidgetActivity.Widgetke.mHandler.postAtTime(这一点,1000);

           }
        };
}

< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      包=com.ibluekey
      安卓版code =1
      机器人:VERSIONNAME =1.0>


    <使用-SDK安卓的minSdkVersion =4/>

    <应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME机器人:可调试=真正的>
        <接收机器人:Reciever名称=机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=MyTestIntent/>
            &所述; /意图滤光器>
        < /接收器>
        <接收器的Andr​​oid版本:NAME =com.ibluekey.WidgetActivity机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.appwidget.action.APPWIDGET_UPDATE/>
            &所述; /意图滤光器>
            &所述;元数据
                机器人:名称=android.appwidget.provider
                机器人:资源=@ XML / widgetprovider/>
        < /接收器>

        <服务机器人:名称=com.ibluekey.WidgetActivity $ UpdateService/>
    < /用途>

< /舱单>
 

解决方案

下面是一个伟大的系列教程中的Andr​​oid创建部件和间隔更新它们。它从这里开始: 的http://android-er.blogspot.com/2010/10/simple-home-screen-app-widget-with.html

和结束在这里有一个定期定时器/报警: http://android-er.blogspot.com /2010/10/app-widget-using-alarm-manager.html

基本上,你需要设置一些事情的清单来侦听广播意图而更新微件。它也展示了如何创建小部件一个配置的活动。

要创建的报警或经常性的活动,你也可以使用AlarmManager类。

I just started to learn Android, I'm trying to write a widget which is updating the counter in every seconds, but somewhere it's missing something.I'm getting 1 on the screen. I'm using Android 4 emulator.

package com.ibluekey;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import com.ibluekey.R;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class WidgetActivity extends AppWidgetProvider {
    public static WidgetActivity Widgetke = null;
    public static Context context;
    public static AppWidgetManager appWidgetManager;
    public static int appWidgetIds[];   
    private Handler mHandler = new Handler();
    private static int count=0; 
    @Override
    public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds )    {     
        if (null == context) context = WidgetActivity.context;
        if (null == appWidgetManager) appWidgetManager = WidgetActivity.appWidgetManager;
        if (null == appWidgetIds) appWidgetIds = WidgetActivity.appWidgetIds;

        WidgetActivity.Widgetke = this;
        WidgetActivity.context = context;
        WidgetActivity.appWidgetManager = appWidgetManager;
        WidgetActivity.appWidgetIds = appWidgetIds;

        Log.d("PXR", "onUpdate");

        final int N = appWidgetIds.length;
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];  

            updateAppWidget(context,appWidgetManager, appWidgetId);            
        }

    }


    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
            int appWidgetId) {

        count++;

        /*Intent intent = new Intent(context, UpdateService.class);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
        remoteViews.setOnClickPendingIntent(R.id.mainlayout, pendingIntent);

        remoteViews.setTextViewText(R.id.test_out, ""+count);

        // Tell the widget manager
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);*/

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
        views.setTextViewText(R.id.test_out, count+"");
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }



    public static class UpdateService extends Service {
        @Override
        public void onStart(Intent intent, int startId) {
            //WidgetActivity.Widgetke.onUpdate(context, appWidgetManager, appWidgetIds);
            //Toast.makeText(context, "Update Widget", Toast.LENGTH_SHORT).show();

            WidgetActivity.Widgetke.mHandler.removeCallbacks(WidgetActivity.Widgetke.mUpdateTimeTask);
            WidgetActivity.Widgetke.mHandler.postDelayed(WidgetActivity.Widgetke.mUpdateTimeTask, 100);
        }

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

    private Runnable mUpdateTimeTask = new Runnable() {
           public void run() {         

               WidgetActivity.Widgetke.onUpdate(context, appWidgetManager, appWidgetIds);              
               WidgetActivity.Widgetke.mHandler.postAtTime(this, 1000);

           }
        };
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.ibluekey"
      android:versionCode="1"
      android:versionName="1.0">


    <uses-sdk android:minSdkVersion="4" />

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
        <receiver android:name=".Reciever" android:label="@string/app_name">
            <intent-filter>
                <action android:name="MyTestIntent" />
            </intent-filter>
        </receiver>  
        <receiver android:name="com.ibluekey.WidgetActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data                
                android:name="android.appwidget.provider"
                android:resource="@xml/widgetprovider" />
        </receiver>

        <service android:name="com.ibluekey.WidgetActivity$UpdateService" />
    </application>

</manifest>

解决方案

Here is a great tutorial series for creating widgets in Android and updating them at intervals. It starts here: http://android-er.blogspot.com/2010/10/simple-home-screen-app-widget-with.html

And ends here with a recurring timer/alarm: http://android-er.blogspot.com/2010/10/app-widget-using-alarm-manager.html

Basically you need to setup some things in the manifest to listen for a broadcasted intent which updates the widget. It also shows how to create a config activity for the widget.

To create alarms or recurring events you can also use the AlarmManager class.

这篇关于之后×时间的Andr​​oid窗口小部件更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆