I want to start service when I turn on my device. If I start Activity, then its working fine but in case of Service its crashed. Please guide me. My code is here. In my LogCat. I am getting this Error ActivityNotFoundException
. How to solve this issue?
My Service
public class StartServiceAfterBoot extends BroadcastReceiver {
public StartServiceAfterBoot() {
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent(context, BackgroundLocationService.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
My Activity
public class MainActivity extends AppCompatActivity {
private Button mStartUpdatesButton;
private Button mStopUpdatesButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intent = new Intent(MainActivity.this, BackgroundLocationService.class);
startService(intent);
}
My Manifest
<service android:name=".LocationUpdates" />
<service android:name=".BackgroundLocationService" />
<receiver
android:enabled="true"
android:exported="true"
android:name="com.locationupdates.broadcast.StartServiceAfterBoot"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>