I’m trying to start a service from my widget provider to update the widget on orientation change. The problem is that the service does not seem to start. Can anyone see what I’m doing wrong? This is all I have in my code for now.
public class PSWidgetProvider extends AppWidgetProvider {
private static Handler handler = new Handler();
private static Runnable runnable;
public static int currentAlarmSet = 0;
@Override
public void onReceive(final Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
context.startService(new Intent(context, MyUpdateService.class));
}
public static class MyUpdateService extends Service
{
@Override
public void onStart(Intent intent, int startId)
{
Log.i("MyUpdateService", "onStart");
super.onStart(intent, startId);
// Update the widget
RemoteViews remoteViews = buildRemoteView(this);
// Push update to homescreen
pushUpdate(remoteViews);
// No more updates so stop the service and free resources
stopSelf();
}
public RemoteViews buildRemoteView(Context context)
{
RemoteViews updateViews = null;
updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_powersleep_layout);
// Your code to build and update the remote view
return updateViews;
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
int oldOrientation = this.getResources().getConfiguration().orientation;
if(newConfig.orientation != oldOrientation)
{
// Update the widget
RemoteViews remoteViews = buildRemoteView(this);
// Push update to homescreen
pushUpdate(remoteViews);
}
}
private void pushUpdate(RemoteViews remoteViews)
{
ComponentName myWidget = new ComponentName(this, PSWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(myWidget, remoteViews);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
}
My Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.am.pn"
android:versionCode="7"
android:versionName="1.1.7" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".activity.PSActivity"
android:label="@string/title_activity_p_s" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".receiver.PSWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.appwidget.action.APPWIDGET_ENABLED"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_config" />
</receiver>
<receiver
android:name=".receiver.AlarmReceiver"
android:process=":remote" >
</receiver>
<service
android:name=".receiver.PSWidgetProvider.MyUpdateService"
android:process=":configservice"
android:enabled="true"
android:exported="true"
android:configChanges="keyboardHidden|orientation" >
</service>
</application>
</manifest>
Figured it out, the service name should have read
android:name=".receiver.PSWidgetProvider$MyUpdateService"since it is a static inner class.