Broadcastreceiver gives error with BOOT_COMPLETED

Asked

Viewed 541 times

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 the Manifest.xml? Edit and enter the code for exception to facilitate the resolution of the problem.

  • 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 ?

  • It may be that your Intent is coming as null. Try commenting on the line if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){ and }, done this test the application. You can also change to if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())).

  • I switched context.startActivity(i) to context.startService(i),now it runs, but it doesn’t call my Alarmreceiver methods..

  • It really hadn’t seen this part. Its AlarmReceiver should not extend the Service and not of BroadcastReceiver? From what I understand you want to use it as a Service, right?

  • 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 ?

  • Take a look at this question that I answered today: http://answall.com/a/137085/11421. It explains how well one works Serviceand how you use it with the device boot.

  • 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, ?

  • 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.

Show 4 more comments

1 answer

2


Question answered in comments, just moving here.


Exchange the context.startActivity(i) for context.startService(i).

As for your class AlarmReceiver, I believe that what you need to update the information is a Service. So you can use your BootReceiverto start the Service when the device boots.


This link will help you with what you need: How to make application running in the background all the time

Browser other questions tagged

You are not signed in. Login or sign up in order to post.