Skip to content

Instantly share code, notes, and snippets.

@wakim
Last active March 15, 2016 01:15
Show Gist options
  • Save wakim/0bf03857735324ff011a to your computer and use it in GitHub Desktop.
Save wakim/0bf03857735324ff011a to your computer and use it in GitHub Desktop.
mServiceIntent = new Intent(getActivity(), RSSPullService.class);
mServiceIntent.setData(Uri.parse(dataUrl));
// Inicia o IntentService
startService(mServiceIntent);
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<!--
Usar android:exported como "false",
faz com que o Service seja visível apenas para esse app.
-->
<service
android:name=".RSSPullService"
android:exported="false"/>
<application/>
public class RSSPullService extends IntentService {
Handler mHandler = new Handler();
@Override
protected void onHandleIntent(Intent workIntent) {
// Recupera a url para consumo de dados
String url = workIntent.getDataString();
// Realiza o trabalho baseado na dataString
Object result = doWorkWithUrl(url);
// Notifica possiveis interessados de que o trabalho terminou.
EventBus.getDefault().post(new WorkDoneEvent(result));
// Ou usando @Subscribe(threadMode = ThreadMode.MAIN) nos consumidores do evento
// Ou caso queira garantir que o evento será disparado na MainThread
// em caso de desacoplamento.
mHandler.post(new Runnable(){
EventBus.getDefault().post(new WorkDoneEvent(result));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment