Reputation: 113
So Simply put I have one widget that is just one big button really, and on clicking it it does something. This works fine, however I now want to add more widgets to the app, they will all just be one button but each when clicked will need to do something else. So far I have tired just copying the code I already have but it seems that when I click one of the widgets which ever I click first all the widgets will from then on do the same thing.
So really my question is how can I change the code to allow lots of widgets, or is there better code I should use (This code was taken from a guide and mashed up to work, I am by no means a Android expert)
Here is the code I have right now.
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);
}
}
}
Upvotes: 0
Views: 143
Reputation: 1834
I believe what you want to do is to add more buttons to R.layout.widget1 then duplicate the code starting at Intent intent = new... once for each button, changing the intent to launch something else. Best regards.
Upvotes: 1