【发布时间】:2011-08-15 06:24:52
【问题描述】:
我是 android 小部件的新手。我做了示例应用程序,想要显示时间。如何从活动中调用小部件:
我试过这样,但它说 Caused by: android.content.ActivityNotFoundException: Unable to find explicit Activity class {com.weather/com.weather.CurrentCity};您是否在 AndroidManifest.xml 中声明了此活动?
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.weather"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="Weather">
<!-- Broadcast Receiver that will process AppWidget updates -->
<receiver android:name=".CurrentCity" 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/hello_widget_provider" />
</receiver>
<activity android:name=".WeatherActivity"
android:label="Weather Application">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
和我的初始活动:
public class WeatherActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent showContent = new Intent(getApplicationContext(),CurrentCity.class);
startActivity(showContent);
}
和我的小部件活动:
public class CurrentCity extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1,
1000);
}
private class MyTime extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.current_weather);
thisWidget = new ComponentName(context, CurrentCity.class);
}
@Override
public void run() {
remoteViews.setTextViewText(R.id.country,"Time = " + format.format(new Date()));
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
}
我已经创建了 main.xml 和 current_weather.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:id="@+id/country"
android:text="TextView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:textColor="@color/solid_yellow"
></TextView>
</RelativeLayout>
我在 res/xml/hello_widget_provider.xml 中创建了 hello_widget_provider.xml.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="10000"
android:initialLayout="@layout/main"
/>
请帮帮我
提前致谢
【问题讨论】:
-
另外,不要使用 TimerTask。这不会像您预期的那样工作。在您的 onUpdate() 中执行所有操作,这将以每 30 分钟的最大速率调用一次。