[英]Launching an activity when a widget is dropped for choose widget
i need make activity for choose widget 我需要进行活动以选择小部件
Widget1.java Widget1.java
public class Widget1 extends AppWidgetProvider{
Context context;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
if (appWidgetIds != null) {
int N = appWidgetIds.length;
for (int mAppWidgetId : appWidgetIds) {
Intent intent = new Intent(context, UpdateWidgetService.class);
intent.putExtra(EXTRA_APPWIDGET_ID, mAppWidgetId);
intent.setAction("FROM WIDGET PROVIDER");
context.startService(intent);
}
}
}
public static class UpdateWidgetService extends IntentService {
public UpdateWidgetService() {
// only for debug purpose
super("UpdateWidgetService");
}
@Override
protected void onHandleIntent(Intent intent) {
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(UpdateWidgetService.this);
int incomingAppWidgetId = intent.getIntExtra(EXTRA_APPWIDGET_ID,
INVALID_APPWIDGET_ID);
if (incomingAppWidgetId != INVALID_APPWIDGET_ID) {
try {
updateNewsAppWidget(appWidgetManager, incomingAppWidgetId,
intent);
} catch (NullPointerException e) {
}
}
}
public void updateNewsAppWidget(AppWidgetManager appWidgetManager,
int appWidgetId, Intent intent) {
Log.v("String package name", this.getPackageName());
RemoteViews views = new RemoteViews(this.getPackageName(),
R.layout.widget1);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
Widget2.java Widget2.java
public class widget2 extends AppWidgetProvider{
Context context;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
if (appWidgetIds != null) {
int N = appWidgetIds.length;
for (int mAppWidgetId : appWidgetIds) {
Intent intent = new Intent(context, UpdateWidgetService.class);
intent.putExtra(EXTRA_APPWIDGET_ID, mAppWidgetId);
intent.setAction("FROM WIDGET PROVIDER");
context.startService(intent);
}
}
}
public static class UpdateWidgetService extends IntentService {
public UpdateWidgetService() {
// only for debug purpose
super("UpdateWidgetService");
}
@Override
protected void onHandleIntent(Intent intent) {
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(UpdateWidgetService.this);
int incomingAppWidgetId = intent.getIntExtra(EXTRA_APPWIDGET_ID,
INVALID_APPWIDGET_ID);
if (incomingAppWidgetId != INVALID_APPWIDGET_ID) {
try {
updateNewsAppWidget(appWidgetManager, incomingAppWidgetId,
intent);
} catch (NullPointerException e) {
}
}
}
public void updateNewsAppWidget(AppWidgetManager appWidgetManager,
int appWidgetId, Intent intent) {
Log.v("String package name", this.getPackageName());
RemoteViews views = new RemoteViews(this.getPackageName(),
R.layout.widget2);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
widget1.xml widget1.xml
<AnalogClock
android:id="@+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:dial="@drawable/hand_dial"
android:hand_minute="@drawable/hand_minute"
android:hand_hour="@drawable/hand_hour"/>
widget2.xml widget2.xml
<AnalogClock
android:id="@+id/analogClock2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:dial="@drawable/hand_dial2"
android:hand_minute="@drawable/hand_minute2"
android:hand_hour="@drawable/hand_hour2"/>
widget1_config.xml widget1_config.xml
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="140dip"
android:minHeight="140dip"
android:previewImage="@drawable/preview"
android:initialLayout="@layout/widget1"
android:updatePeriodMillis="1800000"
android:resizeMode="horizontal|vertical"/>
AndroidManifest.xml AndroidManifest.xml
<receiver android:name=".Widget1" android:label="Analog Clock">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_config" />
</receiver>
<receiver android:name=".widget2" android:label="Analog Clock2">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget2_config" />
i can make ten widget in one app every widget Single , But this is not good 我可以在一个应用程序中使每个小部件Single制作十个小部件,但这不好
so i need Launching activity when a widget is dropped for choose widget 所以当需要删除小部件以选择小部件时,我需要启动活动
How can I do this ? 我怎样才能做到这一点 ?
Finally, I apologize for my English is not good 最后,我为我的英语不好而道歉
Thank you. 谢谢。
You need to add the android:configure
attribute to you widget provider XML. 您需要将
android:configure
属性添加到窗口小部件提供程序XML。 See https://github.com/slightfoot/android-note-widget/blob/master/res/xml/appwidget_note.xml 参见https://github.com/slightfoot/android-note-widget/blob/master/res/xml/appwidget_note.xml
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="com.demondevelopers.notewidget.ConfigActivity"
android:initialKeyguardLayout="@layout/appwidget_note"
android:initialLayout="@layout/appwidget_note"
android:minHeight="110dp"
android:minWidth="180dp"
android:minResizeWidth="40dp"
android:minResizeHeight="40dp"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen"
android:updatePeriodMillis="0"
/>
For full example check out my project: https://github.com/slightfoot/android-note-widget 有关完整示例,请查看我的项目: https : //github.com/slightfoot/android-note-widget
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.