0

I am making an app which has a switch on its main page and when the switch is on then it starts another activity . Here I am using getIntent() in the second activity . But it is not working and givivg the error "Undefined method" .I searched and found that this is due to the reason that my class extends 'service' and not 'activity' .But I still don't understand the solution.

public class OverlayButtonActivity extends Service implements OnTouchListener {
    Button mButton;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        Intent intent = getIntent();

        //mView = new HUDView(this);
         mButton = new Button(this);
         mButton.setText("Overlay button");
         mButton.setOnTouchListener(this);
    }
}

Any help??

Wakim
  • 1,696
  • 2
  • 14
  • 13
Abhimanyu
  • 29
  • 4

2 Answers2

0

Services are given an Intent in their onStart method, there is none available when you're in onCreate.

Dave Morrissey
  • 4,371
  • 1
  • 23
  • 31
0

Maybe you're doing an mistake by extending Service instead of Activity. Looking at your class name and your onCreate method, you're manipulating View elements, that cannot exists inside an Android Service.

The correct should be:

public class OverlayButtonActivity extends Activity implements OnTouchListener {
    // ...
}
Wakim
  • 1,696
  • 2
  • 14
  • 13
  • Thanks a lot.. I understood my mmistake . I have to make another class first which extends activity and that class should call getIntent() . I am going to try it. – Abhimanyu Jun 07 '14 at 19:41
  • then how send data from activity class to service class. Beacon beacon = getIntent().getParcelableExtra(ListBeaconsActivity.EXTRAS_BEACON); i getting error is "The method getIntent() is undefined for ServiceBackGround'" – Prasad Oct 08 '14 at 16:43
  • @Prasad, you don't need to call `getIntent`, just override the `onStartCommand` method or the `onBind` and use the `Intent` argument instance. – Wakim Oct 09 '14 at 00:34