Update more widgets from BroadcastReceiver using RemoteViews - android

I followed these steps: https://stackoverflow.com/a/18236800/824963 to make my broadcastreceiver work.
Actually all alarmmanagers are working fine, but each time onReceive is called all widget are updated with the same data
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(final Context context, Intent intent)
{
final String address = intent.getStringExtra("mykey");
...
final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
// downloading data
...
// decoding data
...
remoteViews.setTextViewText(R.id.mykey, newvalue);
ComponentName thiswidget = new ComponentName(context, AppWidgetProvider.class);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(thiswidget, remoteViews);
}
}

Related

In Appwidget RemoteViews' setOnClickPendingIntent is not getting called after user force stops application

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.

Widget not updating on creation? - Android

I've got a widget that shows weather when a button is clicked on it. The widget is supposed to do this update when it's first created, however that's not the case. I have to manually click-update it once I put it on homescreen.
Q. How to I update the widget automatically once it's created?
Things I've tried:
Use onEnabled inside Widget Provider
Use onRecieve inside Widget Provider
My WidgetProvider:
public class Widget extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.rButton, buildButtonPendingIntent(context));
pushWidgetUpdate(context, views);
}
public static PendingIntent buildButtonPendingIntent(Context context) {
Intent intent = new Intent();
intent.setAction("update");
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void pushWidgetUpdate(Context context, RemoteViews views) {
ComponentName myWidget = new ComponentName(context, Widget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, views);
}
}
My BroadcastReceiver:
public class WidgetReceiver extends BroadcastReceiver {
double lat, lon;
String xtemp, xmaxtemp, xmintemp, xcity, xweather, provider, weatherpic;
Location loc;
LocationManager mlocManager;
//======================================= OnReceive Method
#Override
public void onReceive(Context context, Intent intent) {
try {
if (intent.getAction().equals("update")) {
new MyAsyncTask2().execute(context);
}
} catch (Exception e){
e.printStackTrace();
}
}
public class MyAsyncTask2 extends AsyncTask<Context, Void, String> {
//Does all the update for the weather and shows it on the textViews
}
}
I'm new to Widget programming, let me know if I can make any optimizations! :)

Android: BatteryLevel widget not updating

I'm having some problems using RemoteViews. Simply want to make a widget that shows battery level. I've done all other stuffs already.
Here is my class:
public class BatteryLevel extends AppWidgetProvider{
public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds ){
context.startService(new Intent(context, BatteryMonitorReceiver.class));
}
public static class BatteryMonitorReceiver extends Service{
private int currentBatteryLevel = 0;
private BroadcastReceiver batteryReceiver = new BroadcastReceiver(){
#Override
public void onReceive( Context context, Intent intent ){
currentBatteryLevel = intent.getIntExtra( "level", 0 );
}
};
#Override
public void onStart (Intent intent, int startId) {
registerReceiver( this.batteryReceiver, new IntentFilter( Intent.ACTION_BATTERY_CHANGED ) );
RemoteViews updateViews = new RemoteViews( getPackageName(), R.layout.main );
updateViews.setTextViewText( R.id.level, String.valueOf(currentBatteryLevel));
ComponentName thisWidget = new ComponentName( this, BatteryLevel.class );
AppWidgetManager manager = AppWidgetManager.getInstance( this );
manager.updateAppWidget(thisWidget, updateViews);
}
#Override
public IBinder onBind( Intent intent ){
return null;
}
}
}
When i start the app no error and btw no update at R.id.level.
What's wrong?
Note: api version 7.
Since your service is an inner class, I guess you have not declared it in your Android Manifest, which is needed.
And you shouldn't use onStart, it is deprecated. Use onStartCommand. And you forgot to unregister your receiver...

Battery Widget in Android

Hi all I want to make a battery widget in android with animation. I think I will be able to animate but I want to know that how can I get the battery status again and again? Will it be OK to do it through thread? Or something else is required. Here is the simple code for animation. Please explain how will I get the data from battery again and again all the time.I know the functions but I don't know the mechanism to use them.
public class HelloWidget extends AppWidgetProvider{
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
ImageView img;
Bitmap icon, icon1;
int counter = 0;
#Override
public void onUpdate(Context context, final AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
thisWidget = new ComponentName(context, HelloWidget.class);
remoteViews.setImageViewResource(R.id.imgv, R.drawable.icon2);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
( new Thread()
{
public void run()
{
while(true)
{
if(counter%15>=0 && counter%15 <=7)
{
remoteViews.setImageViewResource(R.id.imgv, R.drawable.icon2);
}
else
{
remoteViews.setImageViewResource(R.id.imgv, R.drawable.icon);
}
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
counter++;
}
}
}
).start();
}
Register broadcast receiver for battery events and don't query every time.
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
mContext.registerReceiver(batteryLevelReceiver, batteryLevelFilter);

Send a public broadcast from an AppWidget in Android

I have a widget for a music player and want to be able to send broadcasts when pushing the different buttons. What I want to do is when a button is pushed, the widget sends a public broadcast to another BroadcastReceiver so it can handle the different actions.
In my activity class with the BroadcastReceiver:
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Constants.ACTION_NEXT)) {
Log.d("RECEIVER", "ACTION_NEXT");
}
}
};
#Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter();
//Widget actions
filter.addAction(Constants.ACTION_NEXT);
registerReceiver(broadcastReceiver, new IntentFilter(filter));
}
In my Widget:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(Constants.ACTION_NEXT), 0);
views.setOnClickPendingIntent(R.id.WidgetNextButton, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
Any help or other solutions are very much appreciated!
EDIT: Forgot to mention my actual problem: The BroadcastReceiver never receive the broadcast

Categories

Resources