2011-10-15 79 views
0

首先,我試着搜索這個ans並不成功。如果我的問題已經回答了,有人可以請我指出它嗎?非常感謝你通過觀察這個來幫助我,或者通過掠過這些並指引我朝着正確的方向。我真的很感激!在AlertDialog中啓動一個服務並綁定一個外部活動

我的困境:我有一個活動,並在該活動有listview。該列表視圖中有多個項目,最需要調用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(); 
                } 
              }); 
            } 
          } 
        }; 
      } 

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

再次注意發生彈出之前發生的綁定。第二,請注意我在綁定中啓動服務。我做錯了什麼或只是沒有得到?

再次謝謝大家, 凱利

+0

你的服務更新數據後你想要做什麼? – Noel

+0

嗨諾埃爾,我想有活動刷新數據列表 – KellyTheDev

回答

0

還有一個更簡單的方法做你想做的事情,不需要綁定服務的。

首先,您應該擴展IntentService而不是Service。這將確保您的服務不會在UI線程上運行。您將通過您目前的相同方式啓動服務。

其次,在您的服務實現中,發送廣播消息以指示您已完成而不是嘗試使用BoundService要容易得多。因此,在你的服務,你會做這樣的事情:

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

然後,在你的活動,你會要註冊,將被通知這些變化BroadcastReciever。有關更多詳細信息,請參閱BroadcastReceiver doc

+0

感謝分享諾埃爾!我要給它一個鏡頭並回報。 – KellyTheDev

相關問題