我正在尝试开发一个小部件,这是三星 Galaxy Note 10.1 上的一大遗漏:一个 S-pen note 小部件。
到目前为止,我刚刚获得了界面,并且只有一个操作(显示和隐藏一些按钮)。在模拟器上它显示一切都很好,但在我的 note10.1 上它只是坐在屏幕上,什么都不做。没有崩溃,但也没有功能。(它也不显示我的烤面包机消息,模拟器也显示得很好)
我的清单中提到了接收器,如下所示:
<receiver android:name="WidgetProvider">
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name="org.Note.Widget.ACTION_widgetClick"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/note_widget"/>
</receiver>
我已经尝试了许多示例作为我的 Java 代码的指南。这就是我现在拥有的:
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(CLK))
{
ToggleRibbon(context);
}
else
{
Toast.makeText(context, "action is: "+intent.getAction(), Toast.LENGTH_LONG).show();
super.onReceive(context, intent);
}
}
static void ToggleRibbon(Context context)
{
RemoteViews localRemoteViews = BuildUpdate(context);
ComponentName localComponentName = new ComponentName(context, WidgetProvider.class);
AppWidgetManager.getInstance(context).updateAppWidget(localComponentName, localRemoteViews);
}
private static RemoteViews BuildUpdate(Context context)
{
RemoteViews rv = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
Intent active = new Intent(context, WidgetProvider.class);
active.setAction(CLK);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
rv.setOnClickPendingIntent(R.id.note_ribbon, actionPendingIntent);
if(ShowRibbon)
{
ShowRibbon = false;
rv.setViewVisibility(R.id.note_ribbon, View.INVISIBLE);
Toast.makeText(context, "You clicked the Note!! (ribbon invisible)", Toast.LENGTH_LONG).show();
}
else
{
ShowRibbon = true;
rv.setViewVisibility(R.id.note_ribbon, View.VISIBLE);
Toast.makeText(context, "You clicked the Note!! (ribbon visible)", Toast.LENGTH_LONG).show();
}
return rv;
}
protected PendingIntent getPendingSelfIntent(Context context, String action)
{
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; ++i)
{
int appWidgetId = appWidgetIds[i];
//Intent intent = new Intent(context, WidgetProvider.class);
//intent.setAction("click");
//PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteViews.setOnClickPendingIntent(R.id.widget_root, getPendingSelfIntent(context, CLK));
remoteViews.setOnClickPendingIntent(R.id.menu_btn, getPendingSelfIntent(context, "menu click"));
//remoteViews.setOnClickPendingIntent(R.id.widget_root, pendingIntent);
//remoteViews.setOnClickPendingIntent(R.id.menu_btn, pendingIntent);
// update widget
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
我的猜测是,这将是我忘记的一些愚蠢的事情,但我真的希望有人可以帮助我让我的小部件做更多的事情,而不仅仅是坐在那里:P(只有烤面包机的消息已经有很大帮助了!)
提前致谢 ;)