关于安卓开发桌面小插件不自动更新的问题
最近自己写一个安卓桌面的电子时钟程序,由于是新手,所以遇到了下面这个问题。本应用是桌面电子时钟,虽然楼主将更新时间设置为1s,但这个桌面小时钟依然不能自动更新时间,代码如下:
EX04_28.java
-----------------------------------------
package com.android.ex04_28;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
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.os.IBinder;
import android.widget.RemoteViews;
public class EX04_28 extends AppWidgetProvider
{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds)
{
Intent intent = new Intent(context, UpdateService.class);
context.startService(intent);
}
public static class UpdateService extends Service
{
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public void onStart(Intent intent, int startId)
{
RemoteViews updateViews = new RemoteViews(this.getPackageName(),
R.layout.main);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss",Locale.CHINA);
updateViews.setTextViewText(R.id.TextView1, "" + sdf.format(new Date()));
ComponentName thisWidget = new ComponentName(this, EX04_28.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
}
}
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:text="@+id/TextView1"
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black">
</TextView>
</LinearLayout>
strings.xml
-------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">EX04_28</string>
<string name="action_settings">Settings</string>
<string name="TextView1"></string>
</resources>
my_widget_provider.xml
---------------------------
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="100dip"
android:minHeight="100dip"
android:updatePeriodMillis="1"
android:initialLayout="@layout/main"
/>
AndroidManifest.xml
---------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.ex04_28"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.android.ex04_28.EX04_28"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:label="@string/app_name"
android:name=".EX04_28">
<intent-filter>
<action
android:name="android.appwidget.action.APPWIDGET_UPDATE">
</action>
</intent-filter>
<meta-data
android:resource="@xml/my_widget_provider"
android:name="android.appwidget.provider">
</meta-data>
</receiver>
<service android:name=".EX04_28$UpdateService" />
</application>
</manifest> Android 应用 --------------------编程问答-------------------- 本人已解决。
补充:移动开发 , Android