hi,
i am newbie to android. i have one mediaplayer class,one
mediaplayeractivity class,one service class and appwidget class.
my problem is that i am able to update activity from widget but from
activity widget is not updated properly.
below code i m using in my activity class to update widget.
Intent launchIntent = getIntent();
Bundle extras =
launchIntent.getExtras();
if(extras!=null)
{
appWidgetId =
extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
// set the result for cancel first
Intent cancelResultValue = new Intent();
cancelResultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
setResult(RESULT_CANCELED,
cancelResultValue);
SharedPreferences prefs =
self.getSharedPreferences("prefs", 0);
SharedPreferences.Editor edit =
prefs.edit();
edit.putLong("goal" + appWidgetId, 0l);
edit.putString("track",Utility.selectedTrackList.get(StreamingMediaPlayer.CURRENTINDEX));
edit.putString("artist",
Utility.selectedTrackArtistList.get(StreamingMediaPlayer.CURRENTINDEX));
edit.commit();
// fire an update to display initial
state of the widget
PendingIntent updatepending =
MediaAppWidgetProvider
.makeControlPendingIntent(self,
BackService.UPDATE, appWidgetId);
try {
updatepending.send();
} catch (CanceledException e) {
e.printStackTrace();
}
// change the result to OK
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
setResult(RESULT_OK, resultValue);
and this is my widget class.
public class MediaAppWidgetProvider extends AppWidgetProvider {
//update rate in milliseconds
public static final int UPDATE_RATE = 1000;
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
setAlarm(context, appWidgetId, -1);
}
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
context.stopService(new Intent(context,BackService.class));
super.onDisabled(context);
}
@Override
public void onUpdate(Context context, AppWidgetManager
appWidgetManager,
int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
setAlarm(context, appWidgetId, UPDATE_RATE);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
public static void setAlarm(Context context, int appWidgetId, int
updateRate) {
PendingIntent newPending =
makeControlPendingIntent(context,BackService.UPDATE,appWidgetId);
AlarmManager alarms = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
if (updateRate >= 0) {
alarms.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime(), updateRate, newPending);
} else {
// on a negative updateRate stop the refreshing
alarms.cancel(newPending);
}
}
public static PendingIntent makeControlPendingIntent(Context context,
String command, int appWidgetId) {
Intent active = new Intent(context,BackService.class);
active.setAction(command);
active.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
//this Uri data is to make the PendingIntent unique, so it
wont be updated by FLAG_UPDATE_CURRENT
//so if there are multiple widget instances they wont override
each other
Uri data =
Uri.withAppendedPath(Uri.parse("MediaAppWidgetProvider://widget/id/
#"+command+appWidgetId), String.valueOf(appWidgetId));
active.setData(data);
return(PendingIntent.getService(context, 0, active,
PendingIntent.FLAG_UPDATE_CURRENT));
}
}
if u need my service class than pls tell me i will post it.
can anyone tell me what is the problem with this code.
thankx
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en