0

This is my code:

public class SimpleWidgetProvider extends AppWidgetProvider {
    BluetoothAdapter mBluetoothAdapter;
    String number="";
    PendingIntent pendingIntent;
    Button b;

    //private static final String MyOnClick = "myOnClickTag";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        //final int count = appWidgetIds.length;

        final int N = appWidgetIds.length;


        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            Intent intent = new Intent(context, SimpleWidgetProvider.class);
            intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.simple_widget);
            views.setOnClickPendingIntent(R.id.actionButton, pendingIntent);

            //views.setOnClickPendingIntent(R.id.button1, getPendingSelfIntent(context, MyOnClick));
            appWidgetManager.updateAppWidget(appWidgetId, views);

            //Toast.makeText(context, "Button Clicked", Toast.LENGTH_SHORT).show();

        }

In my widget there is a button (id:actionButton in the layout), what I want is when I click on this button, bluetooth of the device will turn on and app will show a toast. I was working on the toast part for testing at first but unable to do it. How to do this? As you can see I am using pending intents, is it impossible to do what I want? Or there are other ways? Searched the net at first but didn't get the exact solution, so asking for your suggestions.

enter image description here

  • Check out this link: http://www.compiletimeerror.com/2013/11/turn-on-and-off-bluetooth-in-android.html#.VjtNJ7crKM8 – activesince93 Nov 05 '15 at 12:39
  • Thanks, but that's not what I want. I know how to turn on bluetooth adding listeners, but I am trying to make a control widget here; I cannot add listeners to the button which is inside the widget @activesince93 –  Nov 05 '15 at 12:42
  • 1
    You should visit this Repository. This is an example of **FlashLightWidget**. You will find how to get button `onClick` event. In this example `BroadcastReceiver` has been used. Repository: https://github.com/ChiragSavsani/FlashLightWidget – activesince93 Nov 05 '15 at 12:49
  • @activesince93 big code but useful one. Working on it. Thanks! –  Nov 05 '15 at 13:20
  • @activesince93 Working perfectly! You should comment this one as answer :) –  Nov 05 '15 at 13:51
  • Sure. Please check my answer. – activesince93 Nov 05 '15 at 18:40

2 Answers2

0

Widget is simple control unit. It will not be able to do the complete flow.

My suggestion would be you put a pending intent to trigger the service / activity which will do it.

You can show the Toast also from there.

Vinay
  • 2,395
  • 3
  • 30
  • 35
0

Check out this repository: FlashLightWidget

You will find how to get button onClick event. In this example BroadcastReceiver is used.

activesince93
  • 1,716
  • 17
  • 37