我只需要拥有一个简单的小部件,一个按钮将点击更新文本字段并显示当前时间。
我遵循了几个教程,但我无法让它发挥作用。
在我的情况下,它不会更新文本字段...你能帮助我让它工作吗?
这是我的出发点之一:http://www.tutorialspoint.com/android/android_widgets.htm
Java代码看起来像这样(包含其他XML文件):
package com.example.myname.widget005;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.wifi.p2p.nsd.WifiP2pServiceInfo;
import android.widget.Button;
import android.widget.RemoteViews;
import java.util.Date;
/**
* Implementation of App Widget functionality.
*/
public class Widget005 extends AppWidgetProvider {
Button btnUpdate;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = context.getString(R.string.appwidget_text);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget005);
views.setTextViewText(R.id.appwidget_text, widgetText);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
Date date = new Date();
String currentTime = date.toString();
System.out.println("my test => " + currentTime);
Intent intent = new Intent(context, Widget005.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setTextViewText(R.id.txtInfo, currentTime);
views.setOnClickPendingIntent(R.id.btnUpdate, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myname.widget005" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".Widget005" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget005_info" />
</receiver>
</application>
</manifest>
widget005_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="110dp"
android:minHeight="110dp"
android:updatePeriodMillis="86400000"
android:previewImage="@drawable/example_appwidget_preview"
android:initialLayout="@layout/widget005"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen"
android:initialKeyguardLayout="@layout/widget005">
</appwidget-provider>
widget005.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin"
android:background="#09C">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/appwidget_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/appwidget_text"
android:textColor="#ffffff"
android:textSize="24sp"
android:textStyle="bold|italic"
android:layout_margin="8dp"
android:contentDescription="@string/appwidget_text"
android:background="#09C"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/txtInfo"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/btnUpdate"
android:layout_below="@+id/txtInfo"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
</LinearLayout>
</RelativeLayout>