首先,我试图搜索这个ans是不成功的。如果我的问题已经解决,有人可以指点我吗?非常感谢您通过观察这一点来帮助我,或者甚至通过略读这一点并指出我正确的方向。我真的很感激它!
我的困境:我有一项活动,在该活动中有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 truncated(constructor),此外,它是从AlertDialog.Builder类扩展而来的。我还有一个在所有对话框中都很常见的类,基本上我存储了_context。所以在这种情况下,上下文保存在super中。 _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,注意我在绑定中启动服务。我做错了什么或者没有得到什么?
再次感谢大家, 凯利
答案 0 :(得分:0)
有一种更简单的方法可以执行您不想要绑定服务的操作。
首先,您应该扩展IntentService而不是Service。这将确保您的服务不在UI线程上运行。您可以通过与目前相同的方式启动服务。
其次,在您的服务实现中,发送广播消息以表明您已完成比尝试使用BoundService更容易。因此,在您的服务中,您可以执行以下操作:
Intent intent = new Intent(); // specify the intent filter appropriately here
sendBroadcast(intent);
然后,在您的活动中,您需要注册一个BroadcastReciever,通知您这些更改。有关详细信息,请参阅BroadcastReceiver doc。