我在我的HomeScreen上有一个Widget,我在其上添加了一个Button按钮.我将小部件ID从Widget传递给服务,但是当我在服务中读取WidgetId时,它总是3.
小工具:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent intent = new Intent(context, WidgetService.class);
// prints the correct appWidgetId
System.out.println(appWidgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
remoteView.setOnClickPendingIntent(R.id.Button01, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteView);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
Run Code Online (Sandbox Code Playgroud)
服务看起来像这样:
public int onStartCommand(Intent intent, int flags, int startId){
if (intent != null && intent.getExtras() != null) {
int widgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
// 3 is always …Run Code Online (Sandbox Code Playgroud) 我有一个游戏,并希望增加一些成就.在这个例子中,我们假设有以下内容:
这三项成就共享同一个柜台.但据我所知,我需要创建3个不同的增量成就,并在赢得游戏时发布每个成就.
我看到的唯一另一种选择是,不要做出增量成就并在本地计算.
还有其他建议吗?
我想在类上访问静态方法,但是要在泛型中传递该类.
我做了以下事情:
class Base{
public static String getStaticName(){
return "Base";
}
}
class Child extends Base{
public static String getStaticName(){
return "Child";
}
}
class StaticAccessor{
public static <T extends Base>String getName(Class<T> clazz){
return T.getStaticName();
}
}
StaticAccessor.getName() // --> "Base"
Run Code Online (Sandbox Code Playgroud)
这将返回"基地",但我想要的是"孩子"任何人没有反思的建议吗?