0
所以簡單地說,我有一個小工具真的只是一個大按鈕,點擊它就可以做一些事情。這工作正常,但我現在想要添加更多的小部件到應用程序,他們都將只是一個按鈕,但每個單擊時都需要做別的。 到目前爲止,我已經厭倦了複製我已經擁有的代碼,但是似乎當我點擊其中一個小部件時,我首先會點擊所有小部件,然後再做同樣的事情。多個小工具可以做不同的事情 - Android App
所以真的我的問題是如何更改代碼以允許很多小部件,或者是否有更好的代碼我應該使用(此代碼是從指南中獲取的,並且可以工作,我決不是Android專家)
這是我現在的代碼。
import java.util.Arrays;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
public class ExampleAppWidgetProvider extends AppWidgetProvider {
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
Log.i("ExampleWidget", "Updating widgets " + Arrays.asList(appWidgetIds));
// 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 ExampleActivity
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("jackpal.androidterm", "jackpal.androidterm.Term"));
intent.putExtra("jackpal.androidterm.iInitialCommand", "su \n cd sdcard/backtrack \n sh backtrack.sh");
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.widget1);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// To update a label
// Tell the AppWidgetManager to perform an update on the current app
// widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}