在Activity 中定义了一个数据类变量并初始化:
public class MainActivity extends Activity
{
private DataModel mData=new DataModel();
…
private ServiceConnection mConn=new ServiceConnection()
{
public void onServiceConnected(…)
{
((DataService.DataBinder)service).setData(mData);
}
…
};
…
}
进行服务绑定的时候 Activity 将这个变量的引用传递给Service.
public class DataService extends Service
{
private DataModel mData=null;
class DataBinder extends Binder
{
public void setData(DataModel data)
{
mData=data;
}
public IBinder onBinder (Intent intent )
{
return new DataBinder();
}
}
…
synchronized (mData)
{
…
}
…
}
Service 在接收到新的数据时直接通过这个变量的引用进行操作,使用synchronized 对数据进行同步管理。请问这种做法是否有什么隐患?我在实际使用中会出现刚开始由于数据同步操作导致线程卡顿的现像,然后就看着一切正常了。下步我想在服务里也通过类似传递引用的方式,直接调用界面元素某个Adapter 的notifyDataSetChanged 时出现异常。serviceactivity
public class MainActivity extends Activity
{
private DataModel mData=new DataModel();
…
private ServiceConnection mConn=new ServiceConnection()
{
public void onServiceConnected(…)
{
((DataService.DataBinder)service).setData(mData);
}
…
};
…
}
进行服务绑定的时候 Activity 将这个变量的引用传递给Service.
public class DataService extends Service
{
private DataModel mData=null;
class DataBinder extends Binder
{
public void setData(DataModel data)
{
mData=data;
}
public IBinder onBinder (Intent intent )
{
return new DataBinder();
}
}
…
synchronized (mData)
{
…
}
…
}
Service 在接收到新的数据时直接通过这个变量的引用进行操作,使用synchronized 对数据进行同步管理。请问这种做法是否有什么隐患?我在实际使用中会出现刚开始由于数据同步操作导致线程卡顿的现像,然后就看着一切正常了。下步我想在服务里也通过类似传递引用的方式,直接调用界面元素某个Adapter 的notifyDataSetChanged 时出现异常。serviceactivity
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货