I wrote an AppWidget for my app that you can choose a name of place you like and it shows you a pic from that place.
It has a configuration Activity (I used the same configuration activity of the app itself) when first added, the widget and the it's configuration handeling works fine: I choose a place I like and I see the pic;
Problem is Whenever I restart the device(or it goes out of sleep mode) then when I click on it(the widget) I go into the configuration activity - change to the desired pic and nothing happens when I debug I can see that:
appWidgetManager.updateAppWidget(mAppWidgetId, views);
mAppWidgetId =
has an id
, and views
is not null
.
so what the hack is going on?
my idea is that on restart of the device the widgetId changes, and that I don't handle it like I should.
BTW if try the same on the emulator I have no problems everything works fine
here is my code:
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.dryrun" android:versionCode="3"
android:versionName="1.1" android:installLocation="auto">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/ic_launcher_test"
android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:debuggable="true"><!-- different< android:theme="@style/Theme.NoBackground" -->
<!-- Main Activity -->
<activity android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Preferences -->
<activity android:name=".Preferences.EditPreferences">
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</activity>
<!-- Widgets -->
<!-- Widget-->
<receiver android:name=".Widget.testWidget" android:label="@string/app_widget_">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<!--action
android:name="com.test.dryrun.Widget.testWidget.PREFENCES_WIDGET_CONFIGURE" /-->
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/test_widget__provider" />
</receiver>
<service android:name=".Widget.testWidget$WidgetService" />
<uses-permission android:name="android.permission.BIND_REMOTEVIEWS"></uses-permission>
</application>
</manifest>
appwidget_provide开发者_如何学Cr xml
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="146dip"
android:updatePeriodMillis="0"
android:initialLayout="@layout/test_widget"
/>
Widget Class
public class testWidget extends AppWidgetProvider {
public static String PREFENCES_WIDGET_CONFIGURE = "ActionConfigureWidget";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Intent svcIntent = new Intent(context, WidgetService.class);
widgets = appWidgetIds;
context.startService(svcIntent);
}
@Override
public void onReceive(Context context, Intent intent)
{
// v1.5 fix that doesn't call onDelete Action
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action))
{
final int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
{
this.onDeleted(context, new int[] { appWidgetId });
}
}
else
{
super.onReceive(context, intent);
}
}
//public void updateWidget()
/**
* @param context
* @param remoteViews
*/
public static void updateWidget(Context context, RemoteViews remoteViews)
{
String Prefix = context.getString(R.string._prefix);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String ToShow = prefs.getString(context.getString(
R.string.Widget_string),
context.getString(R.string.default_string));
String pkgName = context.getPackageName();
int resID = context.getResources().getIdentifier(Prefix + ToShow, "drawable", pkgName);
WidgetController widgetController = WidgetController.getInstance();
widgetController.setRemoteViewImageViewSource(remoteViews, R.id.WidgetImage, resID);
}
public static class WidgetService extends Service
{
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
// Update the widget
RemoteViews remoteView = buildRemoteView(this);
// Push update to homescreen
WidgetController.getInstance().pushUpdate(
remoteView,
getApplicationContext(),
testWidget.class);
// No more updates so stop the service and free resources
stopSelf();
}
public RemoteViews buildRemoteView(Context context)
{
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.test_widget);
Intent configIntent = new Intent(context, EditPreferences.class);
configIntent.setAction(testWidget.PREFENCES_WIDGET_CONFIGURE);
configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgets[0]);
//configIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent runtestPendingIntent = PendingIntent.getActivity(context, 0, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.WidgetImage, runtestPendingIntent);
WidgetController controller = WidgetController.getInstance();
controller.updateWidget(context, remoteViews);
return remoteViews;
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
int oldOrientation = this.getResources().getConfiguration().orientation;
if(newConfig.orientation != oldOrientation)
{
// Update the widget
RemoteViews remoteView = buildRemoteView(this);
// Push update to homescreen
WidgetController.getInstance().pushUpdate(
remoteView,
getApplicationContext(),
testWidget.class);
}
}
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
}
}
Prefences class
public class EditPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
Intent intent = getIntent();
m_extras = intent.getExtras();
}
private Bundle m_extras;
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
if(key.equals(getString(R.string.rlvntString)))
{
Context ctx = getApplicationContext();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ctx);
setResult(RESULT_CANCELED);
if (m_extras != null)
{
Intent resultValue = new Intent();
String stringID = AppWidgetManager.EXTRA_APPWIDGET_ID;
mAppWidgetId = m_extras.getInt(
stringID,
AppWidgetManager.INVALID_APPWIDGET_ID);
RemoteViews views = new RemoteViews(ctx.getPackageName(),R.layout.test_widget);
WidgetController.getInstance().updateWidget(ctx, views);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
resultValue.putExtra(stringID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
}
}
}
For future needs. I added a button to my layout xml for my widget.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >
<TextView
android:id="@+id/timeout_title"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textStyle="bold"
android:gravity="center"/>
<!-- ListView to be shown on widget -->
<ListView
android:id="@+id/timeoutListViewWidget"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginEnd="45dp"
android:divider="@null"
/>
<ImageButton
android:id="@+id/timeout_edit"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:src="@drawable/pen"
/>
<!-- Empty view is show if list items are empty -->
<TextView
android:id="@+id/timeOutEmpty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Veri Listesi boş"
android:textColor="#ffffff"
android:textSize="20sp"
android:visibility="gone" />
</LinearLayout>
then I defined onClick listener where I defined remoteviews in my widgetproviders class;
private RemoteViews updateWidgetListView(Context context,
int appWidgetId) {
//which layout to show on widget
RemoteViews remoteViews = new RemoteViews(
context.getPackageName(), R.layout.timeout_widget);
Intent svcIntent = new Intent(context, TimeoutWidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
svcIntent.setData(Uri.parse(
svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter( R.id.timeoutListViewWidget, svcIntent);
remoteViews.setEmptyView(R.id.timeOutEmpty_view, R.id.timeOutEmpty_view);
Intent configIntent = new Intent(context, AppWidgetDepLevelActivity.class);
configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
// Make this unique for this appWidgetId
configIntent.setData(Uri.withAppendedPath(Uri.parse("customuri://widget/id/"), String.valueOf(appWidgetId)));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, configIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.timeout_edit, pendingIntent);
return remoteViews;
}
then in my ConfigurationActivity I created new bundle that carries my config changes;
Bundle newOps = new Bundle();
newOps.putInt("level",0);
and called ;
appWidgetManager.updateAppWidgetOptions(appWidgetId,newOps);
whole function;
private void updateAppWidgetOptions() {
appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
appWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
RemoteViews views = new RemoteViews(getApplicationContext().getPackageName(),
R.layout.timeout_widget);
Bundle newOps = new Bundle();
newOps.putInt("level",0);
appWidgetManager.updateAppWidgetOptions(appWidgetId,newOps);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
}
and this function triggers overrided onAppWidgetOptionsChanged
function in my appwidget provider class and I run my logic there.
here is the function ;
@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
Log.d("APPWIGET","onAppWidgetOptionsChanged");
Object level = newOptions.get("level");
if(level!= null){
Log.d("APPWIGET",level.toString());
}
Date date = new Date();
now = date.getTime();
this.appWidgetId = appWidgetId;
this.appWidgetManager = appWidgetManager;
getTimeOutReportData(context);
}
buildRemoteView must be defined in EditPreferences not in Widget Class. I had the same problem, but after studying this: http://developer.android.com/guide/topics/appwidgets/index.html#Configuring I moved all remoteView buildings to config activity - it's working now
精彩评论