单击时Widget不启动服务

时间:2012-03-03 16:36:26

标签: android android-intent android-service android-appwidget android-pendingintent

我正在为我的应用创建一个小部件,但它拒绝工作。 此小部件在创建时启动配置活动,当您单击它时,它应该启动我创建的IntentService。 但是当我点击它时,它不会启动服务!我试图将此未决意图更改为启动Google的意图并且可以正常运行。

一些代码:

AppWidgetProvider XML

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:configure="com.eladaharon.android.lary.widget.LaryWidgetConfigurationActivity"
    android:initialLayout="@layout/widget_1x1"
    android:minHeight="72dp"
    android:minWidth="72dp"
    android:updatePeriodMillis="86400000" >

</appwidget-provider>

清单声明

<activity
            android:name=".widget.LaryWidgetConfigurationActivity"
            android:configChanges="orientation|keyboard" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
            </intent-filter>
        </activity>

        <service android:icon="@drawable/icon" android:enabled="true" android:name="widget.LaryWidgetCheckService" />

        <!-- Broadcast Receiver that will process AppWidget updates -->
        <receiver android:name=".widget.LaryWidgetProvider" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="android.appwidget.action.APPWIDGET_DELETED" />
                <action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
                <action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_provider" />
        </receiver>

AppWidgetProvider类

public class LaryWidgetProvider extends AppWidgetProvider {
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch service
            Intent intent = new Intent(context, LaryWidgetCheckService.class);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_1x1);
            views.setOnClickPendingIntent(R.id.widget_check_button, pendingIntent);

            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    public void onEnabled(Context context) {
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(
                new ComponentName("com.eladaharon.android.lary.widget",
                        ".SalaryWidgetConfigurationActivity"),
                        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                        PackageManager.DONT_KILL_APP);
    }

    public void onDisabled(Context context) {
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(
                new ComponentName("com.eladaharon.android.lary.widget",
                        ".LaryWidgetConfigurationActivity"),
                        PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                        PackageManager.DONT_KILL_APP);
    }

    public void onDeleted(Context context, int[] appWidgetIds)
    {
        Editor edit = context.getSharedPreferences(Constants.PREFERNCES_WIDGET_PREFERENCES, Context.MODE_PRIVATE).edit();
        final int N = appWidgetIds.length;
        for (int i=0; i<N; i++)
        {
            edit.remove(String.valueOf(appWidgetIds[i]));
        }
        edit.commit();
    }

    public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
            int appWidgetId) {

        // Create an Intent to launch service
        Intent intent = new Intent(context, LaryWidgetCheckService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

        // Get the layout for the App Widget and attach an on-click listener
        // to the button
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_1x1);
        views.setOnClickPendingIntent(R.id.widget_check_button, pendingIntent);

        // Tell the AppWidgetManager to perform an update on the current app widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

}

在配置活动结束时调用的处理程序

private Handler mHandler = new Handler(){

        @Override
        public void handleMessage(Message msg){
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(LaryWidgetConfigurationActivity.this);
            LaryWidgetProvider.updateAppWidget(LaryWidgetConfigurationActivity.this, appWidgetManager,
                    mAppWidgetId);

            // Make sure we pass back the original appWidgetId
            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
            setResult(RESULT_OK, resultValue);
            finish();
        }
    };

我想点击

运行的服务
public class LaryWidgetCheckService extends IntentService {
    private int mAppWidgetId;

    public LaryWidgetCheckService(String name) {
        super("WorkplaceService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(
                    AppWidgetManager.EXTRA_APPWIDGET_ID, 
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        // If they gave us an intent without the widget id, just bail.
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            return;
        }

        // Running some code
    }

}

提前致谢!!

2 个答案:

答案 0 :(得分:4)

我讨厌这个世界! 如果您再次查看清单,您可以注意到我编写了widget.SERVICENAME而不是.widget.SERVICENAME !!!

我花了一整天的时间!

感谢所有试图提供帮助的人!

答案 1 :(得分:0)

Widget在另一个进程中运行,并且您发送的显式意图仅适用于您的应用程序。为什么在网络浏览器中启动谷歌页面的原因是因为这是另一种涉及广播接收器的意图,它在系统范围内传播以查看是否有人做出响应。

我认为您必须在服务中声明广播接收器并从您的小部件中激发该意图。

相关问题