I made an widget which have the image and show the time. howerver , the time cannot update.so I try to implement that the time will be updated when user click on the widget.
I find some tutorial said that using setOnClickPendingIntent can do this but my code not work.
Any solutions can let the widget update the time onClick or Auto Updating?
public class NewAppWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date());;
// Construct the RemoteViews object
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
Intent intent = new Intent(context, NewAppWidget.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
updateViews.setOnClickPendingIntent(R.id.imageView, pendingIntent);
updateViews.setTextViewText(R.id.editText2, widgetText);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
}
#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
}
}
Change you PendingIntent like this
Intent intent = new Intent()
intent.putExtra(EXTRA_VALUES, values)
intent.setComponent(new ComponentName(context, NewAppWidget.class));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Related
I have some buttons in my widget that I want to send a intent extra string and also open my Main Activity. This process seems easy enough when it comes from Activity to Activity but I can't make it work from Widget to Activity. I saw some other answers here but I must be missing something because my Logs are not going off.
Here is my updateAppWidget method:
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.budget_keeper_widget);
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("widget_one", "widget_one");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.button_1_widget, pendingIntent);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
Here's my MainActivity
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String intentFromWidget = intent.getStringExtra("widget_one");
Log.e("WIDGET MAIN INTENT", intentFromWidget);
}
This opens my Activity fine but I get no intent extra in my Logs. What's the trick to making this work?
Thank you!
Hi I am making Android application and I want to make a widget for it. The concept of application is that it gets stt(speech-to-text) string from 'Voicemain_Activity' and put string to created field in 'AddActivity'.And in 'AddActivity' there are a button to call 'Voicemain_Activity'. I was trying to make it to start 'Voicemain_Activity'. So I made widget codes like this.
public class MyAppWidget 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.my_app_widget);
// views.setTextViewText(R.id.appwidget_text, widgetText);
Intent intent=new Intent(context, AddActivity.class);
PendingIntent pe=PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.imageButton_voice, pe);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context,appWidgetManager,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
}
}
the 'AddActivity' that makes to get 'Voicemain_Activity' looks like this.
#Override
public void onClick(View v) {
int view = v.getId();
if(view == R.id.Bacode){
finish();
}
else if(view == R.id.Voice){
startActivityForResult(new Intent(this, VoiceMain_Activity.class), MY_UI);
}
}
But this code made the widget to just show me the 'Voicemain_Activity' screen and didn't get the string nor put it to 'AddActivity'. How can I make it to work?
I have a widget that display a simple text and 3 buttons:
Refresh (Picks another random string and displays it)
Copy (Copy the contents of the textview to the clipboard)
Share (Share the content of the textview to social media and such)
I already Got the refresh button setup and working just fine but I can't seem to figure out a way to handle the other two buttons
PS. I don't need the actual code I already know how to do the copying and sharing I just need to know how to handle click events
Here is my code so far:
Button copy_content;
Button share_content;
void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
/** Code below will be executed once the timer is over*/
String widgetText = RandQuotes[rand.nextInt(RandQuotes.length)];
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.quotes_widget);
views.setTextViewText(R.id.sayings, 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);
}
final int count = appWidgetIds.length;
for (int i = 0; i < count; i++) {
int widgetId = appWidgetIds[i];
String on_sayings = RandQuotes[rand.nextInt(RandQuotes.length)];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.quotes_widget);
remoteViews.setTextViewText(R.id.sayings, on_sayings);
Intent intent = new Intent(context, HavamalWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.switch_trigger, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
Just make pending intents on buttons with action. So in onReceive() just check action of intent and make some logic on that. If you make some long tasks, better to make all logic in IntentService.
Set pending intents on your buttons that are in RemoteViews like this:
public static final String ACTION_BUTTON_SHARE = "ACTION_BUTTON_SHARE";
public static final String ACTION_BUTTON_REFRESH = "ACTION_BUTTON_REFRESH";
Intent refreshIntent = new Intent(context, ExampleAppWidget.class)
.setAction(ACTION_BUTTON_REFRESH);
PendingIntent refreshPI = PendingIntent.getBroadcast(context, 0, refreshIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.refresh_button, refreshPI);
Intent shareIntent = new Intent(context, ExampleAppWidget.class)
.setAction(ACTION_BUTTON_SHARE);
PendingIntent sharePI = PendingIntent.getBroadcast(context, 0, shareIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.share_button, sharePI);
In ExampleAppWidget.class (that extends from AppWidgetProvider) override OnReceive() method
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
if (intent.getAction().equals(ACTION_BUTTON_REFRESH)) {
//make some logic here on button refresh
} else if (intent.getAction().equals(ACTION_BUTTON_SHARE)) {
//make some logic here on button share
} else {
super.onReceive(context, intent);
}
}
}
I have made FlashLight widget. In home screen it's working perfect. When i add it as Lock Screen Widget it's shown but not turning on flashlight by clicking (or not clickable). Can anyone let me know , what mistake ma i doing? I am using HTC ONE m7 with KITKAT updated. Thanks in advance for your help.
Here is the code:
#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];
createWid(context,appWidgetManager,appWidgetId);
}
}
private void createWid(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
// TODO Auto-generated method stub
Intent i0 = new Intent();
i0.setAction("com.cookbook.SMSresponder.AEScreenOnOffService");
context. startService(i0);
Intent receiver = new Intent(context, FlashlightWidgetReceiver.class);
receiver.setAction("COM_FLASHLIGHT");
receiver.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, receiver, 0);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.activity_smsr_responder);
views.setOnClickPendingIntent(R.id.buttonWidgetStopService, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
Below is some code for my widget extending AppWidgetProvider. The widget renders correctly when put on a home screen, but the view is empty. Using logging, I found that no methods are called from the classes I used to extend RemoteViewsService (BudgetWidgetService), which returns an implementation of RemoteViewsService.RemoteViewsFactory.
Is there a mistake causing BudgetWidgetService to never be instantiated or used?
I can't even find a way to check if the BudgetWidgetService is even bound to the RemoteViews layout I create. If you can think of any I can further diagnose this problem, other than logging method calls in my AppWidgetProvider and extension of RemoteViewsService, I would appreciate that as well,
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.i("Budget Widget", "Provider onUpdate called");
// Update each of the widgets with the remote adapter
for (int i = 0; i < appWidgetIds.length; ++i) {
RemoteViews layout = buildLayout(context, appWidgetIds[i]);
appWidgetManager.updateAppWidget(appWidgetIds[i], null);
appWidgetManager.updateAppWidget(appWidgetIds[i], layout);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private RemoteViews buildLayout(Context context, int appWidgetId) {
Log.i("Budget Widget", "Provider buildLayout called");
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_budget);
RemoteViews list_item = new RemoteViews(context.getPackageName(), R.layout.widget_budget_item);
list_item.setTextViewText(R.id.widget_budget_item_text, "Test #1");
final Intent intent = new Intent(context, BudgetWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
rv.setRemoteAdapter(R.id.widget_budget_list, intent);
rv.setEmptyView(R.id.widget_budget_list, R.id.empty_view);
final Intent refreshIntent = new Intent(context, BudgetWidgetProvider.class);
refreshIntent.setAction(BudgetWidgetProvider.REFRESH_ACTION);
final PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(context, 0,
refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
rv.setOnClickPendingIntent(R.id.refresh_button, refreshPendingIntent);
return rv;
}
I was having the same problem, and discovered that I had forgotten to add the android.permission.BIND_REMOTEVIEWS permission to my RemoteViewsService in my AndroidManifest.xml:
<service android:name="MyWidgetService"
...
android:permission="android.permission.BIND_REMOTEVIEWS" />
I found my answer under the section "Manifest for app widgets with collections" on the App Widgets guide.