我试图创建一个动态壁纸,对单击小部件上的按钮做出反应。根据这个How to build a Simple Android Widget我在我的WallpaperService类中重写了onStartCommand,但它似乎没有被调用。这是我的代码:
public class Wallpaper extends WallpaperService {
WallpaperEngine engine;
@Override
public Engine onCreateEngine() {
Log.d("Starting","Engine");
engine = new WallpaperEngine();
return engine;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
Log.d("Starting","command");
engine.actions(intent);
stopSelf(startId);
return START_STICKY;
}
private class WallpaperEngine extends Engine{...}
窗口小部件:
public class InteractionWidget extends AppWidgetProvider {
private static final String FEED = "FeedAction";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.interaction_widget);
Intent intent = new Intent(context,Wallpaper.class);
intent.setAction(FEED);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetIds[i]);
PendingIntent pendingIntent = PendingIntent.getService(context,0,intent,0);
views.setOnClickPendingIntent(R.id.feedButton, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i],views);
updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
public void onReceive(Context context,Intent intent){
if(FEED.equals(intent.getAction())){
}
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = context.getString(R.string.appwidget_text);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.interaction_widget);
views.setTextViewText(R.id.appwidget_text, widgetText);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wallpaper.project.tamwallpaper" >
<uses-feature
android:name="android.software.live_wallpaper"
android:required="true" >
</uses-feature>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service
android:name=".Wallpaper"
android:enabled="true"
android:permission="android.permission.BIND_WALLPAPER" >
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" >
</action>
</intent-filter>
<meta-data
android:name="android.service.wallpaper"
android:resource="@xml/wallpaper" >
</meta-data>
</service>
<receiver android:name=".InteractionWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/interaction_widget_info" />
</receiver>
</application>
</manifest>