我仍然无法在小部件中进行高级操作。假设我有一种方法可以执行诸如打开手电筒之类的操作。我如何使用任何可能的方式从小部件中调用此方法,例如 Async ,服务...我的代码(小部件):
public class QFlashlightWidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch QFlashlightActivity
Intent intent = new Intent(context, ScreenLight.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.qflashlight_appwidget);
views.setOnClickPendingIntent(R.id.btn1, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
请在单击小部件时使用至少可以调用 toast 消息的任何代码支持我。这只是一个小部件,一旦单击就会打开我的活动。