按钮单击无法在Android App小部件中启动服务

时间:2010-05-31 07:17:28

标签: android location android-widget

我无法启动服务来更新我正在创建的AppWidget作为练习。我正在尝试从DDMS获取欺骗性位置数据的纬度和经度以显示在窗口小部件中。小部件使用服务来更新TextView,这可能有些过度,但我想要遵循AppWidgets中常见的模板,这些模板可以做更多的工作(比如Forecast小部件或Wiktionary小部件)。

现在,我没有收到任何错误消息或奇怪的行为;按下按钮时什么也没发生。对于可能出错的事我有点神秘。任何人都可以指出我正确的方向吗?

此外,如果我的位置逻辑有问题,我也会喜欢这方面的建议。我查看了几个博客,Google示例和文档,但我觉得它有点模糊。

以下是小部件的当前状态:

public class Widget extends AppWidgetProvider
{
    static final String TAG = "Widget"; 
    /**
     * {@inheritDoc}
     */
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                        int[] appWidgetIds)
    {
        // Create an intent to launch the service
        Intent serviceIntent = new Intent(context, UpdateService.class);

        // PendingIntent is required for the onClickPendingIntent that actually
        // starts the service from a button click
        PendingIntent pendingServiceIntent = 
            PendingIntent.getService(context, 0, serviceIntent, 0);

        // Get the layout for the App Widget and attach a click listener to the
        // button
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
        views.setOnClickPendingIntent(R.id.address_button, pendingServiceIntent);
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    // To prevent any ANR timeouts, we perform the update in a service;
    // really should have its own thread too
    public static class UpdateService extends Service
    {
        static final String TAG = "UpdateService"; 
        private LocationManager locationManager;
        private Location currentLocation;
        private double latitude;
        private double longitude;

        public void onStart(Intent intent, int startId)
        {
            // Get a LocationManager from the system services
            locationManager = 
                (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            // Register for updates from spoofed GPS
            locationManager.requestLocationUpdates("gps", 30000L, 0.0f, new LocationListener()
            {
                @Override
                public void onLocationChanged(Location location)
                {
                    currentLocation = location;
                }

                @Override
                public void onProviderDisabled(String provider) {}

                @Override
                public void onProviderEnabled(String provider) {}

                @Override
                public void onStatusChanged(String provider, int status,
                        Bundle extras) {}       
            });
            // Get the last known location from GPS
            currentLocation = 
                locationManager.getLastKnownLocation("gps");

            // Build the widget update
            RemoteViews updateViews = buildUpdate(this);

            // Push update for this widget to the home screen
            ComponentName thisWidget = new ComponentName(this, Widget.class);

            // AppWidgetManager updates AppWidget state; gets information about 
            // installed AppWidget providers and other AppWidget related state 
            AppWidgetManager manager = AppWidgetManager.getInstance(this);

            // Updates the views based on the RemoteView returned from the
            // buildUpdate method (stored in updateViews)
            manager.updateAppWidget(thisWidget, updateViews);
        }

        public RemoteViews buildUpdate(Context context)
        {
            latitude = currentLocation.getLatitude();
            longitude = currentLocation.getLongitude();
            RemoteViews updateViews = 
                new RemoteViews(context.getPackageName(), R.layout.main);
            updateViews.setTextViewText(R.id.latitude_text, "" + latitude);
            updateViews.setTextViewText(R.id.longitude_text, "" + longitude);
            return updateViews;
        }

        @Override
        public IBinder onBind(Intent intent) {
            // We don't need to bind to this service
            return null;
        }
    }

}

2 个答案:

答案 0 :(得分:3)

尝试设置

的最后一个参数
  PendingIntent.getService(context, 0, serviceIntent, 0);

为:

  PendingIntent.getService(context, 0, serviceIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

有时你需要将addData添加到Android的意图中,以区别于新的东西,这有点hacky但似乎有用,所以添加:

  serviceIntent.setData(Uri.parse("uri::somethingrandomandunique");

答案 1 :(得分:0)

 PendingIntent pendingIntent = PendingIntent.getService(context, 0, startServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.start_service_btn_widget, pendingIntent);
必须致电

appWidgetManager.updateAppWidget,或点击事件赢得回复

    appWidgetManager.updateAppWidget(componentName, remoteViews);