0

首先,我尝试搜索此 ans 未成功。如果我的问题已经回答了,有人可以指点我吗?非常感谢您通过查看此内容来帮助我,甚至浏览此内容并为我指明正确的方向。我真的很感激!

我的困境:我有一个活动,在那个活动中有列表视图。列表视图中有多个项目,其中最需要调用 AlertDialog。AlertDialog 调用一个服务,该服务说:“嘿,你需要更新一些数据。” 服务侦听并成功更新。

发挥作用的问题是我的活动不承认该服务。我想知道的是,我不完全了解服务的运作方式,以及您是否可以多次启动/运行相同的服务。

请注意,我的示例与The Android Dev Reference for Service的想法有些关联。

这个例子:

    public class MyActivity extends Activity implements IMainActivity 
    {
        ListView _list;

        private RefreshConnector _serviceConnector;

        private boolean _isBound;

        public MyActivity () {}

        public fillList()
            {
                    //this won't trigger within the service
            }

            private void doBindService()
            {
                    // Establish a connection with the service.  We use an explicit
                    // class name because we want a specific service implementation that
                    // we know will be running in our own process (and thus won't be
                    // supporting component replacement by other applications).
                    getApplicationContext().bindService(new Intent(this, UpdateScheduleService.class), _serviceConnector, BIND_AUTO_CREATE);
                    _isBound= true;
            }

            private void doUnbindService()
            {
                    if (_isScheduleBound)
                    {
                            // Detach our existing connection.
                            getApplicationContext().unbindService(_serviceConnector);
                            _isBound= false;
                    }
            }

            @Override
            protected void onDestroy()
            {
                    super.onDestroy();
                    doUnbindService();
            }

            @Override
            public void onCreate(Bundle savedInstanceState)
            {
                    super.onCreate(savedInstanceState);

                    _isBound= false;

                    _list = (ListView) findViewById(R.id.TheList);
                    _list.setOnItemClickListener(new AdapterView.OnItemClickListener()
                    {
                            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
                            {
                                    if (view != null)
                                    {
                                            CheckBox selectedFlag = (CheckBox) view.findViewById(R.id.selectedItem);
                                            selectedFlag.setOnClickListener(new View.OnClickListener()
                                            {
                                                    public void onClick(View view)
                                                    {
                                                           doBindService();
                                                           Bundle extras = new Bundle();
                                                           extras.putBoolean(BundleLocations.BUNDLE_ENABLED, ((CheckBox) view).isChecked());
                                                           extras.putLong(BundleLocations.BUNDLE_SCHEDULE_ID, 123); //123 is an example of an id being passed
                                                           extras.putString(BundleLocations.ACTION, BundleLocations.ACTION_SELECTED);
                                                           Intent updateSelection = new Intent("ChangeItems");
                                                           updateSelection.putExtras(extras);
                                                           view.getContext().startService(updateSelection);
                                                    }
                                           });

                                           TextView description = (TextView) view.findViewById(R.id.description);
                                           description.setText(s.getDescription());
                                           description.setOnClickListener(new View.OnClickListener()
                                           {
                                                    public void onClick(View view)
                                                    {
                                                             doBindService();
                                                             ChangeDescriptionDialog dia = new ChangeDescriptionDialog(view.getContext());
                                                             dia.setTitle(view.getContext().getResources().getString(R.string.DESCRIPTION_TITLE));
                                                             dia.setAction(BundleLocations.ACTION_DESCRIPTION);
                                                             dia.setDescription("something new"); //simplified for example...
                                                             dia.create();
                                                             dia.show();
                                                    }
                                           });
                                    }
                            }
                    };
            }

刷新连接器:

public class RefreshConnector implements ServiceConnection
{
    private UpdateService service;

    public IMainActivity getActivity()
    {
        return activity;
    }

    public void setActivity(IMainActivity value)
    {
        this.activity = value;
    }

    public UpdateScheduleService getService()
    {
        return service;
    }

    private IMainActivity activity;

    public void onServiceConnected(ComponentName componentName, IBinder iBinder)
    {
        service = ((UpdateService.UpdateBinder)iBinder).getService();

        if(activity != null)
            activity.fillList();
    }

    public void onServiceDisconnected(ComponentName componentName)
    {
        service = null;
    }
}

更新服务

public class UpdateService extends Service
{
    final public IBinder binder = new UpdateBinder();

    @Override
    public IBinder onBind(Intent intent)
    {
        return binder;
    }

    public class UpdateBinderextends Binder
    {
        public UpdateService getService()
        {
            return UpdateService .this;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        if (intent == null)
        {
            stopSelf();
            return START_STICKY;
        }

        if(action.equals(BundleLocations.ACTION_SELECTED))
        {
            long id = intent.getExtras().getLong(BundleLocations.BUNDLE_ID);
            boolean enabled = intent.getExtras().getBoolean(BundleLocations.BUNDLE_ENABLED);

            if(id < 0 )
            {
                return START_STICKY;
            }

            String item = intent.getExtras().getString(BundleLocations.BUNDLE_ITEM);

            if (item == null || action.equals("")) return START_STICKY;

            //do some work with saving the description, which it does

            return START_STICKY;
        }

        return START_STICKY;
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }
}

DescriptionDialog 被截断(构造函数),此外,这是从 AlertDialog.Builder 类扩展而来的。我还有一个在所有对话框中都很常见的类,基本上我存储_context。所以在这里的情况下,上下文保存在super.xml中。_context 受保护...

public ChangeDescriptionDialog(Context context)
    {
        super(context);

        DialogInterface.OnClickListener onClickListenerPositive = new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
                //dispatch change
                Intent ChangeItems = new Intent("ChangeItems");
                Bundle extras = new Bundle();
                    extras.putParcelable(BundleLocations.BUNDLE_ITEM, _description);
                extras.putString(BundleLocations.ACTION, _action);
                ChangeItems.putExtras(extras);

                //
                // my question is partly here
                // I start this service...
                //
                // How would my activity see it... right now it doesn't seem that way even though I do the binding before this call
                //
                _context.startService(ChangeItems);
        }
    };

    this.setPositiveButton(context.getResources().getString(R.string.DIALOG_POS), onClickListenerPositive);
}

同样,请注意在弹出窗口发生之前发生的绑定。2、注意我在绑定中启动服务。我做错了什么或只是没有得到?

再次感谢大家,凯利

4

1 回答 1

0

有一种更简单的方法可以做你想做的事情,不需要绑定服务。

首先,您可能应该扩展 IntentService 而不是 Service。这将确保您的服务不会在 UI 线程上运行。您将通过与当前相同的方式启动服务。

其次,在您的服务实现中,发送广播消息以表明您已完成比尝试使用 BoundService 要容易得多。因此,在您的服务中,您会执行以下操作:

Intent intent = new Intent(); // specify the intent filter appropriately here
sendBroadcast(intent);

然后,在您的活动中,您可能希望注册一个 BroadcastReciever 来通知这些更改。有关详细信息,请参阅BroadcastReceiver 文档。

于 2011-10-15T23:18:27.677 回答