0
I need to update every 1h the information and I’m using a BroadCastReceiver
.
If the user opens the APP, it works properly doing everything every 1h.
But I need it to do with the phone boot and not only when opening the app.
But I do not know why, says that my application stopped after the phone call. I don’t know where the mistake is, since he arrives at the end of the methods.
I’m calling the AlarmService
thus:
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent i = new Intent(context,AlarmReceiver.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
In my AlarmService
is the following code :
http://pastebin.com/F9yxhLsx
For Logcat, I see he’s coming in until he gets to the end of the code.
CHEGOU FINAL ANTES abrir arquivo
CHEGOU FINAL ANTES ESCREVER
CHEGOU FINAL
Where could the mistake be? Thank you in advance for your attention!
You declared the permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
in theManifest.xml
? Edit and enter the code forexception
to facilitate the resolution of the problem.– Vitor Henrique
Yes, I put the permission and the app is installed in Ternal.It is not giving any Exception, simply stop responding. Does it have anything to do with context.startActivity ?
– saidmrn
It may be that your
Intent
is coming asnull
. Try commenting on the lineif (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
and}
, done this test the application. You can also change toif("android.intent.action.BOOT_COMPLETED".equals(intent.getAction()))
.– Vitor Henrique
I switched context.startActivity(i) to context.startService(i),now it runs, but it doesn’t call my Alarmreceiver methods..
– saidmrn
It really hadn’t seen this part. Its
AlarmReceiver
should not extend theService
and not ofBroadcastReceiver
? From what I understand you want to use it as aService
, right?– Vitor Henrique
Thanks for the help, I do not know much the difference of the 2,what would change from one to the other ? ,I will try to switch to service,if I put service,would not give error in the receiver manifest by not extending Broadcastreceiver ?
– saidmrn
Take a look at this question that I answered today: http://answall.com/a/137085/11421. It explains how well one works
Service
and how you use it with the device boot.– Vitor Henrique
Hello, thank you worked perfectly, how could I adapt to call the service every x hours ? I’ve heard it’s with Alarmmanager and I’ve looked around, ?
– saidmrn
Okay, buddy, I’ll put it as an answer so other people can see. As for your other question, it would be nice for you to create a new question with this subject.
– Vitor Henrique