Reputation: 2957
I tried to configure my application to receive even on Application startup. This code doesn`t work (even the log messages are not logged)
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".BackgroundService"
android:enabled="true"
android:exported="false"/>
</application>
<receiver android:name=".StartupReceiver" >
<!-- This intent filter receives the boot completed event -->
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
And my receiver
public class StartupReceiver extends BroadcastReceiver {
public StartupReceiver() {
Log.e("Tag", "STARTUP construct");
}
@Override
public void onReceive(Context context, Intent intent) {
// start your service here
Log.e("Tag", "STARTUP");
context.startService(new Intent(context, BackgroundService.class));
}
}
Can you see any mistakes?
Upvotes: 0
Views: 351
Reputation: 2633
You've made a mistake in your manifest. The receiver should be declared inside the <application>
tag.
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".BackgroundService"
android:enabled="true"
android:exported="false"/>
<receiver android:name=".StartupReceiver" >
<!-- This intent filter receives the boot completed event -->
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Upvotes: 1
Reputation: 4185
You need to use the full namespace for your receiver name:
<receiver android:name="com.whatever.StartupReceiver">
Upvotes: 0