0

此处的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()这里使用该方法,以便生成通知。到目前为止,我还无法做到这一点。我似乎找不到获取服务实例的正确方法。

如何才能做到这一点?

4

0 回答 0