嗨,我是Android开发新手。我想用java让我的android手机保持沉默。搜索谷歌后,我发现了一些例子,但他们没有为我工作。这是我的代码。
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);
}
}
有人可以帮我解决这个问题吗?
提前致谢
更新
这是我的清单文件
<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>
我更新了清单文件
答案 0 :(得分:6)
您必须将此权限添加到清单文件中:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" ></uses-permission>
答案 1 :(得分:6)
试试这个:
AudioManager audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audio.setRingerMode(0);
清单文件:
<application>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
</application>
答案 2 :(得分:3)
也许您需要在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>