我是新到Android widget.I做示例應用程序,即要顯示的時間。如何從活動部件撥打:如何呼籲活動部件(窗口小部件從活動調用)
我想這樣的,但它說引起:android.content.ActivityNotFoundException:無法找到顯式活動類{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" />
和我initail活動:
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分鐘的最大速率調用的。 – swinefeaster