1

Hi I am new to android development. I am trying to make my android phone silent using java. After searching google I found some examples but they didnt workd for me. Here is my code.

package com.zafar.silent;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;

public class Silent extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    AudioManager audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
}

Can someone help me how can I make this run?

Thanks in advance

Update

Here is my menifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zafar.silent"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".Silent"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
</application>

</manifest>

I updated menifest file

Om3ga
  • 30,465
  • 43
  • 141
  • 221
  • How did you add WRITE_SECURE_SETTINGS , as it is not allowed in any application? –  Feb 08 '15 at 05:20

3 Answers3

6

You have to add this permission into the manifest file:

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" ></uses-permission>
Dinesh
  • 6,500
  • 10
  • 42
  • 77
6

try this :

 AudioManager audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    audio.setRingerMode(0);

manifest file:

<application>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
</application>
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Thanks Imran. I did what you said but I am getting this warning "ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.zafar.silent/.Silent } ActivityManager: Warning: Activity not started, its current task has been brought to the front" – Om3ga Apr 28 '12 at 05:32
  • if you are testing on device first uninstall previous app from device and install new – ρяσѕρєя K Apr 28 '12 at 05:35
  • I uninstalled it this time. No error but still it does not make my mobile phone silent. – Om3ga Apr 28 '12 at 05:38
  • Ok I was wrong it is working. But the problem is why vibration doesnt work? – Om3ga Apr 28 '12 at 05:40
  • 1
    for vibration use `audio.setRingerMode(1);` i shucked becuase ths code pefectlly working on my toggle widget – ρяσѕρєя K Apr 28 '12 at 05:42
  • Are you sure you need any special permissions to call `AudioManager.setRingerMode`? [This reference](http://www.android-permissions.org/permissionmap.html) (which seems fairly comprehensive) doesn't list that method as needing permissions. – Ted Hopp Jul 25 '12 at 23:16
  • android.permission.MODIFY_AUDIO_SETTINGS seems to be enough, on Android 4.3 at least – personne3000 Sep 23 '13 at 22:25
3

Maybe you need to include the following permissions in your AndroidManifest...

<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" ></uses-permission>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" ></uses-permission>
wattostudios
  • 8,666
  • 13
  • 43
  • 57