0

I have used the below code to start the service.

Start Service Code:

  Intent serviceIntent = new Intent(this, MyService.class);
            serviceIntent.putExtra(mConstants.PREF_CHANNEL_NAME, channelName);
            startService(serviceIntent);

MyService Code:

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
     if (intent != null) {
                    // Get the channel name from the string extras
                    String channelName = intent
                            .getStringExtra(mConstants.PREF_CHANNEL_NAME);
     } else {
                    mAppUtilInstance.showToastMessage(mContext,
                            "No channel name found."); 
     }
     return super.onStartCommand(intent, flags, startId);
  }

When I quit my application the service will be recreated. It will be again calling its onStartCommand Method. Now I want to get the extra data from an intent. But the intent will be null. How to store and restore the intent data in service class.

Please help me on this.

M Vignesh
  • 1,586
  • 2
  • 18
  • 55

1 Answers1

-1

Use this in onStartCommand

  String data=(String) intent.getExtras().get("data"); 

Edit:

As for getting the intent back, there's a flag for START_REDELIVER_INTENT that will redeliver the intent.

Fahim
  • 12,198
  • 5
  • 39
  • 57