0

まず、この ans を検索しようとしましたが、失敗しました。私の質問がすでに回答されている場合、誰かが私にそれを指摘してもらえますか? これを見て、またはこれをざっと読んで正しい方向に向けてくれたことで、ここで私を助けてくれてありがとう. 本当に感謝しています!

私のジレンマ:私にはアクティビティがあり、そのアクティビティにはリストビューがあります。リストビューには、AlertDialog を呼び出す必要がある複数の項目が含まれています。AlertDialog は、「データを更新する必要があります」というサービスを呼び出します。Service はリッスンし、更新を正常に実行します。

発生する問題は、私のアクティビティがサービスを認識しないことです。私が疑問に思っているのは、サービスがどのように動作するか、および同じサービスを複数回スピンアップ/実行できるかどうかを完全には理解していないということです。

私の例は、 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();
                                                    }
                                           });
                                    }
                            }
                    };
            }

RefreshConnector :

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

UpdateService

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を保存するすべてのダイアログで共通のクラスもあります。したがって、この場合、コンテキストはスーパーに保存されます。_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

バインドされたサービスを必要としない、やりたいことを行うためのはるかに簡単な方法があります。

まず、おそらく Service ではなく IntentService を拡張する必要があります。これにより、サービスが UI スレッドで実行されなくなります。現在と同じ方法でインテントを介してサービスを開始します。

第 2 に、サービスの実装では、BoundService を使用するよりも、ブロードキャスト メッセージを送信して完了を示す方がはるかに簡単です。したがって、サービスでは次のようにします。

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

次に、アクティビティで、これらの変更が通知される BroadcastReciever を登録します。詳細については、BroadcastReceiver のドキュメントを参照してください。

于 2011-10-15T23:18:27.677 に答える