0

enter code hereTrying to start the service but it is not started on Booting while service is running perfectly during the start of the app for first time.

This is the Receiver and i want use switch case because there is lot many things i have to start using receiver.

 @Override public void onReceive(Context context, Intent intent) { Intent serviceIntent; switch (intent.getAction()) { case SmackService.NEW_MESSAGE: Log.i("TAG", "processMessage() BroadCast Recieve"); break; case Intent.ACTION_BOOT_COMPLETED: Log.i("TAG", "Boot"); Toast.makeText(context, "case", Toast.LENGTH_SHORT).show(); serviceIntent = new Intent(context, SmackService.class); context.startService(serviceIntent); break; } } 

Manifest Content :-

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <receiver android:name="com.example.abhishek.im.ChatReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="com.tlt.rx.msg" /> </intent-filter> </receiver> 

Service Class

public class SmackService extends Service { @Override public void onCreate() { super.onCreate(); } @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { user_data = new HashMap<>(); if (intent != null && intent.hasExtra(BUNDLE_MSG_SYNC)) { doChatSync = intent.getBooleanExtra(BUNDLE_MSG_SYNC, false); } if (intent != null) { String email, password, name; email = intent.getExtras().getString("email"); password = intent.getExtras().getString("password"); name = intent.getExtras().getString("name"); activity=intent.getExtras().getString("activity"); user_data.put("email", email); user_data.put("password", password); user_data.put("name", name); start(user_data); } return Service.START_STICKY; } } 

Starting the service manually:

Toast.makeText(MainActivity.this, "Signing in", Toast.LENGTH_LONG).show(); Intent intent = new Intent(MainActivity.this, SmackService.class); intent.putExtra("email", email); intent.putExtra("password", password); intent.putExtra("name", name); intent.putExtra("activity", "MainActivity"); startService(intent); 

nt);

11
  • Permissions go outside of the <application> tags, if you don't have it there. Commented Jun 20, 2016 at 7:41
  • <uses-permission> is outside of <application> tags so if any other problem please let me know @MikeM. Commented Jun 20, 2016 at 7:55
  • Do you have any activities in your application? Have you started the app manually before booting your device? Commented Jun 21, 2016 at 8:54
  • Also , post the code you use to start the Service manually, Commented Jun 21, 2016 at 8:55
  • yes my app is having activities and i am starting the service manually for the first time. Code is edited : @DavidWasser Commented Jun 22, 2016 at 10:32

1 Answer 1

1

Pretty clear what is happening. On boot, your BroadcastReceiver is getting called. It then starts the Service like this:

serviceIntent = new Intent(context, SmackService.class); context.startService(serviceIntent); 

in onStartCommand(), you do this:

email = intent.getExtras().getString("email"); 

Since there are no "extras" in the Intent, getExtras() returns null and you call getString() on a null reference, which then throws a NullPointerException, causing your Service to crash.

End of story.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.