Bicknos
Bicknos

Reputation: 21

Boot_complete broadcast not working

I have a boot_completed broadcast in my app, but it isn't working. The app is not installed on the SD card.

Android manfest.xml

<receiver android:name="BootReceiver"
        android:enabled="true"
     android:exported="true">
     <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED" />

        </intent-filter> 
    </receiver>

Bootreceiver.java

package nl.bicknos.TWPD;

import android.content.BroadcastReceiver; import android.content.Context;
import android.content.Intent;
 public class BootReceiver extends BroadcastReceiver{
    @Override   public void onReceive(Context context, Intent intent) {
        Toast.makeText(this, "Gestart", Toast.LENGTH_SHORT).show();} }

I've search and the solutions I found there didn't work

Upvotes: 0

Views: 138

Answers (3)

Bicknos
Bicknos

Reputation: 21

I found the problemen, it wasn't the code, but my phone. There wasn't a single app that started een my device booted. Since i was running a beta version, I downgraded and now is it werking.

Upvotes: 0

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20656

First of all check if you have implemented in your Manifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Your intent-filter should be

 <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
 </intent-filter>

On your broadcast you have to add

 @Override
public void onReceive(Context context, Intent intent) {
   Toast.makeText(context, "Gestart", Toast.LENGTH_SHORT).show();
}

Note : I guess your mistake was trying to pas this when you've got no context.

EDIT

Also you can try to make a Log.d instead of Toast put :

Log.d("Restarted", "I'm on BR");

If your BroadcastReceiver is not calling then try to replace your manifest receiver to this :

<receiver android:name="nl.bicknos.TWPD.BootReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver>

It's obvious but maybe there's the error... your <uses-permission> needs to be a child of the element...

Let me know if it works :)

Upvotes: 2

JVN
JVN

Reputation: 239

Your mistake is in

Toast.makeText(this, "Gestart", Toast.LENGTH_SHORT).show(); this here does not belong to any UI Activity. Your receiver is actually getting called. But as you are "Toast"ing not in the UI context, you are not able to see the Toast message. if you try to add a log in this onReceive() , you will see it coming in the "logcat".

I hope this helps :)

Upvotes: 0

Related Questions