Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Android
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Android
What is this "this"
Peter Sorotokin
Ranch Hand
Posts: 36
1
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi
There is a Android source code that I understand it amd know what it does except the role of "this" as a parameter to a method .
The code :
package org.example.audio; import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; public class Audio extends Activity implements OnClickListener { private MediaPlayer mp; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setVolumeControlStream(AudioManager.STREAM_MUSIC); findViewById(R.id.button_1).setOnClickListener(this); findViewById(R.id.button_2).setOnClickListener(this); findViewById(R.id.button_3).setOnClickListener(this); findViewById(R.id.button_4).setOnClickListener(this); findViewById(R.id.button_5).setOnClickListener(this); findViewById(R.id.button_6).setOnClickListener(this); findViewById(R.id.button_7).setOnClickListener(this); findViewById(R.id.button_8).setOnClickListener(this); findViewById(R.id.button_9).setOnClickListener(this); } public void onClick(View v) { int resId; switch (v.getId()) { case R.id.button_1: resId = R.raw.button_1; break; case R.id.button_2: resId = R.raw.button_2; break; case R.id.button_3: resId = R.raw.button_3; break; case R.id.button_4: resId = R.raw.button_4; break; case R.id.button_5: resId = R.raw.button_5; break; case R.id.button_6: resId = R.raw.button_6; break; case R.id.button_7: resId = R.raw.button_7; break; case R.id.button_8: resId = R.raw.button_8; break; case R.id.button_9: resId = R.raw.button_9; break; default: resId = R.raw.button_1; break; } // Release any resources from previous MediaPlayer if (mp != null) { mp.release(); } // Create a new MediaPlayer to play this sound mp = MediaPlayer.create(this, resId); mp.start(); } }
In lines 18 to 26 , what is the role of "this" ??? To which it refers ??? Is it the strange "context" ???
Thanks for your help
Ulf Dittmer
Rancher
Posts: 43081
77
posted 10 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
"this" in
Java
refers to the object in which it is used, in this case to the "Audio" class. The method probably takes an Activity or a Context as parameter.
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Can not view the video
Why the program not run?
stucked in this context and useds[]
Sound only plays once
Android code to add two numbers
More...