[英]How to call widget on the Activity (Widget calling from Activity)
我是 android 小部件的新手。我做了示例應用程序,想要顯示時間。如何從活動中調用小部件:
我試過這樣,但它說原因:android.content.ActivityNotFoundException:無法找到明確的活動 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.Z0F635D0E0F3874FFF8B781C132E6C7
<?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"
/>
請幫我
提前致謝
你試圖做的事情實際上是不可能的。 你來做這件事:
Intent showContent = new Intent(getApplicationContext(),CurrentCity.class);
startActivity(showContent);
您不能以這種方式調用小部件:小部件沒有您可以顯示的活動。
您需要非常密切地關注http://developer.android.com/guide/topics/appwidgets/index.html上的教程:但我確實無法將您的代碼轉換為 asuccess 的工作方式指南擁有制作工作小部件所需知道/做的一切。
編輯:請注意,您的小部件必須添加到小部件容器(可能是主屏幕)中,並且不能以編程方式添加。 Android 設備的用戶必須手動操作。
我試圖以許多不同的方式做到這一點。 但是,在 Android 中沒有意圖或類似的替代方案,它可以通過活動(我的意思是應用程序)調用小部件。 您必須拖動小部件並將其放在主屏幕上。
但是,是的,其他方式也是可能的!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.