Hola.
Antes tenia una calse que extendia de Activity. Por cambios en el diseño, esta Activity ha pasado a extender de un Fragment. El problema es que ahora recibo el siguiente error:
04-17 09:35:10.227: E/AndroidRuntime(9377): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.aldakur.instalacionesdep.services.action.FIN flg=0x10 } in com.aldakur.instalacionesdep.info.RssAvisosFragment$ProgressReceiver@42721b68
04-17 09:35:10.227: E/AndroidRuntime(9377): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:776)
04-17 09:35:10.227: E/AndroidRuntime(9377): at android.os.Handler.handleCallback(Handler.java:733)
04-17 09:35:10.227: E/AndroidRuntime(9377): at android.os.Handler.dispatchMessage(Handler.java:95)
04-17 09:35:10.227: E/AndroidRuntime(9377): at android.os.Looper.loop(Looper.java:136)
04-17 09:35:10.227: E/AndroidRuntime(9377): at android.app.ActivityThread.main(ActivityThread.java:5146)
04-17 09:35:10.227: E/AndroidRuntime(9377): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 09:35:10.227: E/AndroidRuntime(9377): at java.lang.reflect.Method.invoke(Method.java:515)
04-17 09:35:10.227: E/AndroidRuntime(9377): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
04-17 09:35:10.227: E/AndroidRuntime(9377): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
04-17 09:35:10.227: E/AndroidRuntime(9377): at dalvik.system.NativeStart.main(Native Method)
04-17 09:35:10.227: E/AndroidRuntime(9377): Caused by: java.lang.NullPointerException
04-17 09:35:10.227: E/AndroidRuntime(9377): at com.aldakur.instalacionesdep.info.RssAvisosFragment$ProgressReceiver.onReceive(RssAvisosFragment.java:123)
04-17 09:35:10.227: E/AndroidRuntime(9377): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:766)
Sé que para que este error no saltase, en el metodo onPause de la Activity debía de escribir el siguiente código:
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (isRegistered) {
Log.e("killed", "RssAvisosActivirty: isRegistered is true. pasaremos a false");
unregisterReceiver(rcv);
isRegistered = false;
}
}
Pero ahora, tratándose de un Fragment no sé donde debo de gestionar eso.
Adjunto código de mi Fragment.
private ProgressDialog pDialog3;
private ArrayList<Aviso> avisosList = new ArrayList();
public ListView lv;
private ProgressReceiver rcv = new ProgressReceiver();
private boolean isRegistered = false;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if(!isMyServiceRunning()){
Log.e("killed", "RssAvisosActivirty: Servicio parado");
//Toast.makeText(getBaseContext(), "Servicio parado. select", Toast.LENGTH_SHORT).show();
//Select
DataBaseHelper myDB = new DataBaseHelper(getActivity().getBaseContext());
myDB.openDataBase();
avisosList = myDB.getAllAvisos("eu");
myDB.close();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.rss_avisos, container, false);
ListView lv = (ListView)view.findViewById(R.id.rss_avisos_lv);
lv.setOnItemClickListener(this);
if(isMyServiceRunning()){
pDialog3 = new ProgressDialog(getActivity());
pDialog3.setMessage("3.Cargando Noticias...");
pDialog3.setCancelable(false);//erabiltzaileak atzera botoia sakatuz ez kantzelatzeko
pDialog3.setProgressStyle(ProgressDialog.STYLE_SPINNER);//barrarewn estiloa. Espiral bat izango d
pDialog3.show();
}
IntentFilter filter = new IntentFilter();
filter.addAction(CheckNuevosAvisosIntentService.ACTION_PROGRESO);
filter.addAction(CheckNuevosAvisosIntentService.ACTION_FIN);
//ProgressReceiver rcv = new ProgressReceiver();
getActivity().registerReceiver(rcv, filter);
AvisosEnListaAdapter adapter = new AvisosEnListaAdapter(getActivity(), avisosList);
lv.setAdapter(adapter);
return view;
}
public class ProgressReceiver extends BroadcastReceiver {
//ProgressDialog pDialog2 = new ProgressDialog(RssAvisosActivity.this);
@Override
public void onReceive(Context context, Intent intent) {
Log.e("Servicio", "RssAvisosActivirty: Dentro de onReceive - ProgressReceive");
//ListView lv = (ListView)view.findViewById(R.id.rss_avisos_lv);
if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_PROGRESO)) {
int prog = intent.getIntExtra("progreso", 0);
//Log.e("Servicio", "progreso "+prog+"");
Log.e("Servicio", "RssAvisosActivirty: Cargando...BroadcastReceiver");
if(!pDialog3.isShowing()){
pDialog3.setMessage("2.Cargando Noticias...");
pDialog3.setCancelable(false);//erabiltzaileak atzera botoia sakatuz ez kantzelatzeko
pDialog3.setProgressStyle(ProgressDialog.STYLE_SPINNER);//barrarewn estiloa. Espiral bat izango d
pDialog3.show();
}
//pbarProgreso.setProgress(prog);
}
else if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_FIN)) {
Log.e("Servicio", "RssAvisosActivirty: FIN. Servicio terminado.");
//Select
DataBaseHelper myDB = new DataBaseHelper(getActivity().getBaseContext());
myDB.openDataBase();
//Log.e("Servicio", "RSSAvisosActivuty: recogiendo noticias. Idioma: "+lang+"");
avisosList = myDB.getAllAvisos("eu");
AvisosEnListaAdapter adapter = new AvisosEnListaAdapter(getActivity(), avisosList);
lv.setAdapter(adapter);
if(pDialog3.isShowing()){
pDialog3.dismiss();
}
}
}
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (CheckNuevosAvisosIntentService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Básicamente lo que hace es lo siguiente. Cuando se debe de mostrar en pantalla el Fragment, si el servicio sigue ejecutandose, muestra un ProgressDialog. Si el servicio esta terminado, muestra el layout del Fragment.
Muchas gracias y un saludo!