How to pass a value in Services?

前端 未结 2 1009
清酒与你
清酒与你 2021-01-16 19:55

I am making a media player ,who can run in background. i have to send a string uri in this service. but i cant send the uri value from my activity through bundle.Services cl

相关标签:
2条回答
  • 2021-01-16 20:07

    If you are trying to send a string or String-array from one Activity to another this can be done in the Intent.

    In ClassA:

    Intent intent = new Intent(this, ClassB);
    tring[] myStrings = new String[] {"test", "test2"};
    intent.putExtra("strings", myStrings);
    startActivity(intent);

    In ClassB:

    public void onCreate() {
    Intent intent = getIntent();
    String[] myStrings = intent.getStringArrayExtra("strings");
    }

    0 讨论(0)
  • 2021-01-16 20:19

    You should use IntentService http://developer.android.com/reference/android/app/IntentService.html.

    Implement onHandleIntent(Intent intent) which will be called each time you start service with the startService(intent). You can pass your data to service through this intent.

    0 讨论(0)
提交回复
热议问题