-2

please tell me what i'm doing wrong

NewAppWidget.java class

package com.example.kinjolnath.alert;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

/**
 * Implementation of App Widget functionality.
 */

public class NewAppWidget extends AppWidgetProvider {
    private static final String TAG = NewAppWidget.class.getSimpleName();

void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {
    Log.d(TAG, "Inside updateAppWidget");
    Log.i(TAG, "Inside updateAppWidget");
    Log.e(TAG, "Inside updateAppWidget");


//        Toast.makeText(context,
//                "Should appear at widget click", Toast.LENGTH_SHORT).show();

    // Construct the RemoteViews object
    Intent intent = new Intent(context, MapsActivity.class);

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

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);

    views.setOnClickPendingIntent(R.id.imageButton, pendingIntent);
    Intent sendingIntent = new Intent(context, SendingService.class);
    sendingIntent.setAction(SendingService.ACTION_SEND_MESSAGE);
    PendingIntent sendingPendingIntent = PendingIntent.getService(context, 0, sendingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.imageButton, sendingPendingIntent);
    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views);
}


@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    Log.d(TAG, "Inside onUpdate");
    Log.i(TAG, "Inside onUpdate");
    Log.e(TAG, "Inside onUpdate");
    SendingService.startActionSendMessage(context);
    for (int appWidgetId : appWidgetIds) {
        updateAppWidget(context, appWidgetManager, appWidgetId);
    }
}

@Override
public void onEnabled(Context context) {
    // Enter relevant functionality for when the first widget is created
    Log.d(TAG, "Inside onEnabled");
    Log.i(TAG, "Inside onEnabled");
    Log.e(TAG, "Inside onEnabled");
}

@Override
public void onDisabled(Context context) {
    // Enter relevant functionality for when the last widget is disabled
    Log.d(TAG, "Inside onDisabled");
    Log.i(TAG, "Inside onDisabled");
    Log.e(TAG, "Inside onDisabled");
    }
}

SendingService.java class

package com.example.kinjolnath.alert;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by kinjolnath on 01-06-2017.
 */

public class SendingService extends IntentService{
    private static final String TAG = NewAppWidget.class.getSimpleName();
    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
 * @param name Used to name the worker thread, important only for debugging.
 */
public static final String ACTION_SEND_MESSAGE = "com.example.MapsActivity.action.send_message";

public SendingService(String name) {
    super(name);
}

public static void startActionSendMessage(Context context){
    Intent intent = new Intent(context, SendingService.class);
    intent.setAction(ACTION_SEND_MESSAGE);
    context.startService(intent);
    Log.d(TAG, "Inside startActionSendMessage");
    Log.i(TAG, "Inside startActionSendMessage");
    Log.e(TAG, "Inside startActionSendMessage");
}

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    if (intent != null){
        final String action = intent.getAction();
        Log.d(TAG, action + "inside onHandleIntent");
        if(ACTION_SEND_MESSAGE.equals(action)){
            handleActionSend();
        }
    }
}
//handleActionSend method:
private void handleActionSend() {
    Toast.makeText(this, "Message sent", Toast.LENGTH_SHORT).show();
    Log.d(TAG, "Inside handleActionSend");
    Log.i(TAG, "Inside handleActionSend");
    Log.e(TAG, "Inside handleActionSend");
    }
}

I followed a tutorial from Udacity and ended up at this stage but it still doesn't work I am new to android development so any help would be appreciated.

1615903
  • 32,635
  • 12
  • 70
  • 99
kinjol
  • 13
  • 2

2 Answers2

2

You can not show Toast on Clicking of WidGet because the widget extends AppWidgetProvider so that class not able to call current Context

If you want to show Toast when create widget or select widget is possible by using bellow code:

Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context,"hiii dhruv", Toast.LENGTH_LONG).show();
            }
        });

==========================================================================

How to user above code in your widget class

@Override
    public void onUpdate(final Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context,"hiii dhruv", Toast.LENGTH_LONG).show();
            }
        });

    }

====================================================================

or

 @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        Toast.makeText(context, "hiiiiiiiiiiii", Toast.LENGTH_SHORT).show();
    }
ND1010_
  • 3,743
  • 24
  • 41
-1

Please see this: Android: Toast in a thread

If toast is not showing then its probably not using the UI thread. You can try using applicationContext instead of this, though I'm not sure that will work. I don't like to run Toasts when the UI isn't visible but have run across this to use for specific testing without having to look at logs.

CmosBattery
  • 934
  • 7
  • 13