Android:无法从WidgetProvider启动服务

时间:2012-08-12 05:46:26

标签: java android android-widget android-service

我正在尝试从我的窗口小部件提供程序启动服务,以更新方向更改窗口小部件。问题是该服务似乎没有启动。谁能看到我做错了什么?这就是我现在在代码中的全部内容。

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;
        }
    }

}

我的宣言:

<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>

1 个答案:

答案 0 :(得分:0)

想出来,服务名称应该是android:name=".receiver.PSWidgetProvider$MyUpdateService" 因为它是一个静态的内部类。