如何在Intent Service中获取Activity

时间:2014-03-24 06:53:06

标签: android android-intent intentservice

我想在意向服务中获得活动。在意向服务中,将数据填充到列表控件。 当我在FloatSomeService(IntentService)中调用DictionaryListAdapter时,不要获取活动 (FloatSomeService.java)服务

 public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();   
    windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
    // Find Controls
    LayoutInflater inflater = LayoutInflater.from(this);        
    viewFloat = inflater.inflate(R.layout.float_view, null);        
    listview = (ListView)viewFloat.findViewById(R.id.listDic);  
    this.generateData();

    // *** Error : When create adapter, get activity from base context *** //
    myAdapter = new DictionaryListAdapter((Activity)getBaseContext(), myListItem);

    listview.setAdapter(myAdapter);
    ......................
    windowManager.addView(viewFloat, parameters);
}

(适用DictionaryListAdapter.java)

public class DictionaryListAdapter extends BaseAdapter{
private Activity myContext;
private ArrayList<HistoryListItem> myItems;
public DictionaryListAdapter(Activity activity, ArrayList<DictionaryListItem> items){
    this.myContext = activity;
    this.myList = items;
}

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {
        LayoutInflater layoutInflater = this.myContext.getLayoutInflater();
        convertView = layoutInflater.inflate(R.layout.history_list_item, null);
    }
    ImageView imgPerson = (ImageView)convertView.findViewById(R.id.imgPerson);
    ...........................
}}

4 个答案:

答案 0 :(得分:1)

我使用了一些技巧来进行列表控制数据绑定。我用LayoutInflater解决了。如果我得到布局inflater,我可以将数据填充到控件。 Intent Service可以获得Layout Inflater。我将LayoutInflater从intent服务发送到我的DictionaryListAdapter类。

对我来说没问题。

(FloatSomeService.java)意图服务

public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();   
    windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
    // Find Controls
    LayoutInflater inflater = LayoutInflater.from(this);        
    viewFloat = inflater.inflate(R.layout.float_view, null);        
    listview = (ListView)viewFloat.findViewById(R.id.listDic);  
this.generateData();

// *** Send Inflater to List Adapter
myAdapter = new DictionaryListAdapter((LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE), myListItem);
listview.setAdapter(myAdapter);
    ......................
    windowManager.addView(viewFloat, parameters);}

(DictionaryListAdapter.java)列表适配器

public class DictionaryListAdapter extends BaseAdapter{
private LayoutInflater layoutInflater;
private ArrayList<HistoryListItem> myItems;
    public DictionaryListAdapter(LayoutInflater inflater, ArrayList<DictionaryListItem> items){
        this.layoutInflater = inflater;
        this.myList = items;
    }

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {
       // don't need activity for layout inflater
        convertView = this.layoutInflater.inflate(R.layout.history_list_item, null);
    }
    ImageView imgPerson = (ImageView)convertView.findViewById(R.id.imgPerson);
    ...........................
}}

答案 1 :(得分:1)

使用广播通知将数据从服务传递到活动,并在收到广播通知时更新活动中的视图。

例如,在服务类中定义一个braoadcast通知函数,

public void sendBroadcastNotification(Bundle extras) {
        if (CoreApplication.DEBUG)
            Log.d(TAG, "Sending broadcast notification" + mIntentMsgId);
        Intent intentBroadcast = new Intent(BROADCAST_MESSAGE_NAME);
        intentBroadcast.putExtra(CoreConstants.EXTRA_INTENT_MSG_ID,
                mIntentMsgId);

        sendBroadcast(intentBroadcast);
    }

并在服务类

中设置这样的通知
sendBroadcastNotification(extras)

在您的活动中定义接收者类

private BroadcastReceiver gpsBRec = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

           //Implement UI change code here once notification is received
      }
}

在活动类中,在onResume()中注册接收器并在onStop()中取消注册接收器

@Override
    public void onStop() {
        super.onStop();
        try {
            unregisterReceiver(gpsBRec);
        } catch (IllegalArgumentException e) {

        }

    }



@Override
    public void onResume() {
        super.onResume();

        registerReceiver(gpsBRec, new IntentFilter(
                RetrieveLastTrackDBService.BROADCAST_MESSAGE_NAME));


    }

答案 2 :(得分:0)

您无法更新服务中的UI。因此您必须使用活动

答案 3 :(得分:-1)

asiya所述,您无法更新服务中的UI。 尝试使用ResultReceiver

通知您服务中的活动