在BroadcastReceiver中使用服务的结果

时间:2013-08-30 08:46:11

标签: android service broadcastreceiver

这里的Service代码从RSS Feed获取一些项目。然后生成项目列表。方法getItems()是揭​​示所带来内容的方法。

public class RssService extends Service {

    ThisBinder mBinder = new ThisBinder();      
    private List<Item> items;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {  
    //Hid some code for brevity
    Log.i("items", Arrays.deepToString(items.toArray()));   
    return Service.START_NOT_STICKY;
}


@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

List<Item> getItems(){
    return items;
}



class ThisBinder extends Binder{
    RssService getService() {
        return RssService.this;
    }

}}

BroadcastReceiver此处每15分钟启动一次服务。

public class RssReciever extends BroadcastReceiver {
        Long REPEAT_TIME = 15L * 60L * 1000L;
        //  Long REPEAT_TIME = 1000L;
        RssService rssService;
        IBinder binder;
        private Context mContext;
        @Override
        public void onReceive(Context context, Intent intent) {
            mContext = context;
            AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            Intent i = new Intent(context, StartServiceReceiver.class);
            PendingIntent pending = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
            Calendar cal = Calendar.getInstance();              
            cal.add(Calendar.SECOND, 30);               
            service.setInexactRepeating(AlarmManager.RTC_WAKEUP,  cal.getTimeInMillis(), REPEAT_TIME, pending);
Toast.makeText(mContext, "Connected, total Items, " + rssService.getItems().size(), Toast.LENGTH_SHORT).show();
        }
}

我需要在这里使用RssService.getItems()方法,以便我可以生成通知。到目前为止,我还没能做到这一点。我似乎无法找到获得服务实例的正确方法。

如何做到这一点?

0 个答案:

没有答案