I just started learning Android Studio. I want to display a toast notification by pressing a button on the widget. However, there is an error when using the getApplicationContext() function. Can you tell me how to fix it? Other things, including getBaseContext(), were the same..... The error message was "Cannot resolve method" getApplicationContext () "" I didn't speak English, so I used Google Translator.... I'm so sorry if you can't understand...
package com.jhjsapp.widget;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;
/**
* Implementation of App Widget functionality.
*/
public class TestWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = context.getString(R.string.appwidget_text);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.test_widget);
views.setTextViewText(R.id.appwidget_text, widgetText);
// 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
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
#Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
#Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
public void butcli(View view){
Toast.makeText(getApplicationContext(),"버튼 눌림", Toast.LENGTH_SHORT).show();
}
}
This is JAVA code
enter image description here
You cannot show Toast message using getApplicationContext() because the Widget extends AppWidgetProvider. So you cannot get the current context.
You can try something like this :
public class testWidget extends AppWidgetProvider{
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, MainPage.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
views.setOnClickPendingIntent(R.id.button1, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Toast.makeText(context, "Button Clicked", Toast.LENGTH_SHORT).show();
}
create a global variable first
private Context context;
then initialize context by adding this line to updateAppWidget() and onUpdate() and onEnabled() and onDisabled();
this.context=context;
then use toast like :
Toast.makeText(context,"버튼 눌림", Toast.LENGTH_SHORT).show();
Related
Here i have made one widget and added its click listener where i'm starting one service.It works perfectly.But once user force stops application from appinfo my widget's click is not responding.I m not getting why this is happening.Help on this will be appreciated.Thank you
My app widget class code:
MyWidget.java:
public class MyWidget extends AppWidgetProvider {
static Context cont;
static SharedPreferences preferences;
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
cont = context;
Intent intent2 = new Intent();
intent2.setAction("....");
PendingIntent pendingIntent = PendingIntent.
getBroadcast(context, 0,
intent2, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget);
views.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent);
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
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
context.startService(new Intent(context, MyService.class));
}
#Override
public void onEnabled(Context context) {
.................. }
#Override
public void onDisabled(Context context) {
.................}
Always use an explicit Intent whenever possible. Your code would not work on Android 8.0 and higher, where implicit broadcasts are banned. And an explicit Intent can move your app out of the stopped state.
I am trying to create a simple widget. I wanted that, when touchet, to show a toast. But it does not show anything. I don't know where I'm wrong, it looks quite well(maybe i have forgotten something i didn't notice).
Code:
static String clAc = "CLICKED";
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
Intent intent = new Intent(context, LocationWidget.class);
intent.setAction(clAc);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent,0);
Paper.init(context);
String title = Paper.book().read("content");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.location_widget);
views.setTextViewText(R.id.appwidget_text, title);
views.setOnClickPendingIntent(R.id.layout_wrapper, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
#Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
#Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if(intent.getAction().contentEquals(clAc))
Toast.makeText(context, "clicked",Toast.LENGTH_LONG).show();
}
Try to update not each appWidgetId, but all appWidgetIds with your logic.
I need to create Widgets in my android app similar work flow of contact widgets in android. But the problem facing is if there are n widgets of the same application each widgets have different functionality. how to differentiate the button click on each widgets.?
public class MyWidgetActivity extends AppWidgetProvider {
private static final String MyOnClick = "myOnClickTag";
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = MyWidgetActivityConfigureActivity.loadTitlePref(context, appWidgetId);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.simple_widget);
views.setTextViewText(R.id.appwidget_text, widgetText);
views.setOnClickPendingIntent(R.id.actionButton, getPendingSelfIntent(context, MyOnClick,views.getLayoutId()));
// 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
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
// Tell the AppWidgetManager to perform an update on the current app
// widget
}
}
#Override
public void onDeleted(Context context, int[] appWidgetIds) {
// When the user deletes the widget, delete the preference associated with it.
for (int appWidgetId : appWidgetIds) {
MyWidgetActivityConfigureActivity.deleteTitlePref(context, appWidgetId);
}
}
#Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
#Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (MyOnClick.equals(intent.getAction()))
{
int appWidgetId = intent.getIntExtra(EXTRA_APPWIDGET_ID,0);
Log.e("KOKOKOKO :: ",""+appWidgetId);
MyWidgetActivityConfigureActivity.loadIdPrefCallQuick(context,appWidgetId);
//your onClick action is here
Toast.makeText(context,"Quick Action Triggered",Toast.LENGTH_SHORT).show();
}
super.onReceive(context, intent);
}
static PendingIntent getPendingSelfIntent(Context context, String action, int appWidgtId)
{
// if we brodcast than definetly we have to define ONRECEIVE
Intent intent = new Intent(context, MyWidgetActivity.class);
intent.putExtra(EXTRA_APPWIDGET_ID,appWidgtId);
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
}
Instead of this code (static PendingIntent getPendingSelfIntent) :
return PendingIntent.getBroadcast(context, 0, intent, 0);
You should do this :
return PendingIntent.getBroadcast(context, appWidgtId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
This way you should get unique intent and also unique appWidgtId.
It is important to set requestCode (2nd attribute of getBroadcast), so it wouldn't conflict.
I'm a newbie and i'm finding difficulty in getting texts from a string array. I tried creating a widget in my app and i wanted the widget to update it's text view from the string array i declared in the resources folder, but this does not seams to work out. Here is my code.
MainActivity.Java
public class MainActivity extends AppCompatActivity {
public static TextView Quote;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Quote = (TextView) findViewById(R.id.appwidget_text);
final String[] Quotes = getResources().getStringArray(R.array.Qoutes);
Random rand = new Random();
int randNum = rand.nextInt(Quotes.length);
Quote.setText(Quotes[randNum]);
}}
NewAppWidget.java
public class NewAppWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = MainActivity.Quote.getText().toString();
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
views.setTextViewText(R.id.appwidget_text, widgetText);
// 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
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0 , intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
remoteViews.setOnClickPendingIntent(R.id.interfaceID,pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
#Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
#Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}}
Thank You
This question already has answers here:
android widget on click event
(2 answers)
Closed 7 years ago.
After clicking on the widget, my code launches a new activity. This is how I have done
public class ExampleAppWidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
for(int i=0; i<appWidgetIds.length; i++){
int currentWidgetId = appWidgetIds[i];
String url = "http://www.tutorialspoint.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse(url));
PendingIntent pending = PendingIntent.getActivity(context, 0,
intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget1);
views.setOnClickPendingIntent(R.id.button, pending);
appWidgetManager.updateAppWidget(currentWidgetId,views);
// Toast.makeText(context, "widget clicked ", Toast.LENGTH_SHORT).show();
}
}
}
I don't want to launch that activity, rather I just want to simply show a Toast near the widget area.
I can I do this..?
Helpp!!
EDIT
My question does not have the answer in this question android widget on click event
However I tried using this code also
public class ExampleAppWidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Toast.makeText(context, "widget clicked ", Toast.LENGTH_SHORT).show();
}
}
But This shows toast only when the widget is placed on the home screen by user, toast is not shown when the user clicks the widget & nothing happens, onUpdate is also not called then !
public class ExampleAppWidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Toast.makeText(context, "widget clicked ", Toast.LENGTH_SHORT).show();
}
}