How to call widget on the Activity (Widget calling from Activity)
I am new to android widget.I did sample application , that want to show time .How to call widget from activity:
I tried like this but It say Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.weather/com.weather.CurrentCity}; have you declared this activity in your 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" />
and my initail activity :
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);
}
and my widget activity:
public class CurrentCity extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Timer timer = new T开发者_如何学运维imer();
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);
}
}
}
and I have created 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>
and I created hello_widget_provider.xml.xml in the res/xml/hello_widget_provider.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"
/>
Please help me
Thanks in advance
What you are trying to do is effectively impossible. You do this:
Intent showContent = new Intent(getApplicationContext(),CurrentCity.class);
startActivity(showContent);
You can not invoke a widget this way: a widget does not have an Activity you can show.
You need to follow very closely the tutorial at http://developer.android.com/guide/topics/appwidgets/index.html: there is really no succinct way I can think of to convert your code into a working widget, but the guide has everything you need to know/do to make a working widget.
EDIT: note that your widget must be added to the widget container (probably the home screen) and can not be added programmatically. The user of the Android device must do it manually.
I tried to do this in many different ways. But, in Android there is no intent or similar alternative, which can invoke a widget via activity (I mean application). You have to drag the widget and drop it on your home screen.
But, yes, other ways it is possible!
精彩评论