C# Corner
Tech
News
Videos
Forums
Trainings
Books
Events
More
Interviews
Jobs
Live
Learn
Career
Members
Blogs
Challenges
Certifications
Bounties
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to Set Ringer Mode, Vibrate Mode & Silent Mode Through AudioManager in Android
WhatsApp
Abhijeet Singh
5y
41.1k
0
1
100
Article
Take the challenge
setringer.rar
Procedure
Start the Eclipse IDE.
Make an activity MainActivity.java.
Make a XML file activity_main.xml.
In XML file,3 Buttons should be there.
The code is given below.
MainActivity.java
package
com.example.setringer;
import
android.media.AudioManager;
import
android.os.Bundle;
import
android.provider.MediaStore.Audio;
import
android.app.Activity;
import
android.content.Context;
import
android.view.Menu;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
public
class
MainActivity
extends
Activity {
Button b1, b2, b3;
AudioManager am;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
b1.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
am.setRingerMode(
0
);
}
});
b2.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
am.setRingerMode(
1
);
}
});
b3.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
am.setRingerMode(
3
);
}
});
}
}
Activity_main.xml
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:tools
=
"http://schemas.android.com/tools"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:paddingBottom
=
"@dimen/activity_vertical_margin"
android:paddingLeft
=
"@dimen/activity_horizontal_margin"
android:paddingRight
=
"@dimen/activity_horizontal_margin"
android:paddingTop
=
"@dimen/activity_vertical_margin"
tools:context
=
".MainActivity"
>
<
TextView
android:id
=
"@+id/textView1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"@string/hello_world"
/>
<
Button
android:id
=
"@+id/button1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_below
=
"@+id/textView1"
android:layout_marginTop
=
"78dp"
android:layout_toRightOf
=
"@+id/textView1"
android:text
=
"silent"
/>
<
Button
android:id
=
"@+id/button2"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignLeft
=
"@+id/button1"
android:layout_centerVertical
=
"true"
android:text
=
"vibrate"
/>
<
Button
android:id
=
"@+id/button3"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignLeft
=
"@+id/button2"
android:layout_below
=
"@+id/button2"
android:layout_marginTop
=
"30dp"
android:text
=
"ringer"
/>
</
RelativeLayout
>
Output
People also reading
Membership not found