我对java和java的编程很新。到目前为止,它很有趣!我制作了一个可以工作的数字时钟小部件,除了它没有更新时间。经过一些研究,我用不同的方法重写了代码。现在,我没有时间出现在TextView布局中。任何人都可以帮我弄清问题是什么?
这是我的整个ClockWidgetProvider.java
package com.acsoft.bsiclock;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.widget.RemoteViews;
import com.acsoft.bsiclock.R;
/**
* The ClockWidgetProvider class is the widget place holder for the ClockWidget.
*/
public class ClockWidgetProvider extends AppWidgetProvider {
/**
* Inner class representing the update service.
*/
public static class ClockUpdateService extends Service {
private static final String ACTION_UPDATE =
"com.cmwmobile.android.samples.clock.action.UPDATE";
private final static IntentFilter intentFilter;
static {
intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_TIME_TICK);
intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
intentFilter.addAction(ACTION_UPDATE);
}
/**
* BroadcastReceiver receiving the updates.
*/
private final BroadcastReceiver clockChangedReceiver = new
BroadcastReceiver() {
/**
* {@inheritDoc}
*/
public void onReceive(Context context, Intent intent) {
updateTime(context);
}
};
/**
* {@inheritDoc}
*/
public IBinder onBind(Intent intent) {
return null;
}
/**
* {@inheritDoc}
*/
public void onCreate() {
super.onCreate();
registerReceiver(clockChangedReceiver, intentFilter);
}
/**
* {@inheritDoc}
*/
public void onDestroy() {
super.onDestroy();
unregisterReceiver(clockChangedReceiver);
}
/**
* {@inheritDoc}
*/
public void onStart(Intent intent, int startId) {
if (intent != null && intent.getAction() != null) {
if (intent.getAction().equals(ACTION_UPDATE)) {
updateTime(this);
}
}
}
}
/**
* Method buildUpdate builds the RemoveViews to be displayed.
* @param context used to retrieve resources.
* @return RemoveViews instance.
*/
private static RemoteViews buildUpdate(Context context) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
String currentDate = DateFormat.getDateInstance(DateFormat.LONG).format(new Date());
views.setTextViewText(R.id.date,currentDate );
String hour = "hh";
SimpleDateFormat.getTimeInstance().format(new Date());
views.setTextViewText(R.id.hour, hour);
String minute = "mm";
SimpleDateFormat.getTimeInstance().format(new Date());
views.setTextViewText(R.id.minute, minute);
String ampm = "a";
SimpleDateFormat.getTimeInstance().format(new Date());
views.setTextViewText(R.id.ampm, ampm);
return views;
}
/**
* {@inheritDoc}
*/
public void onDisabled(Context context) {
super.onDisabled(context);
context.stopService(new Intent(context,
ClockUpdateService.class));
}
/**
* {@inheritDoc}
*/
public void onEnabled(Context context) {
super.onEnabled(context);
context.startService(new Intent(
ClockUpdateService.ACTION_UPDATE));
}
/**
* {@inheritDoc}
*/
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
context.startService(new Intent(
ClockUpdateService.ACTION_UPDATE));
}
/**
* Method updateTime updates the time.
* @param context used to retrieve resources.
*/
private static void updateTime(Context context) {
RemoteViews remoteViews = buildUpdate(context);
ComponentName clockWidget = new ComponentName(context,
ClockWidgetProvider.class);
AppWidgetManager appWidgetManager = AppWidgetManager.
getInstance(context);
appWidgetManager.updateAppWidget(clockWidget, remoteViews);
}
}
这是我的布局xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/clockbr"
android:orientation="vertical"
android:padding="5dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
tools:context=".Bioshock_Infinite_Clock_4x2" >
<TextView
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="1dp"
android:textColor="#4c4439"
android:textSize="12sp"
android:typeface="serif" />
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="33dp" >
<TextView
android:id="@+id/hour"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".305"
android:gravity="right"
android:textColor="#4c4439"
android:textSize="26sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="@+id/minute"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".365"
android:gravity="center"
android:textColor="#4c4439"
android:textSize="26sp"
android:textStyle="bold"
android:typeface="serif" />
<!-- <TextView
android:id="@+id/am_pm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".32"
android:gravity="left"
android:textColor="#4c4439"
android:textSize="24sp"
android:textStyle="bold"
android:typeface="serif" /> -->
<TextView
android:id="@+id/ampm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".32"
android:gravity="left"
android:textColor="#4c4439"
android:textSize="24sp"
android:textStyle="bold"
android:typeface="serif" />
</LinearLayout>
</LinearLayout>
我没有看到任何错误是我的logcat,因为我可以告诉。我已经尝试了一些不同的东西到SimpleDateFormat,但仍然没有运气。希望我遗漏一些简单的东西!
感谢任何帮助!