我正在使用名为“BetterCut”的应用程序(与“AnyCut”相同)。
在该应用程序中,我可以创建活动,系统切换器的快捷方式(但振动切换不在命题中),...
我还可以使用三个值创建自己的快捷方式:Action
,Data
和Type
。问题是我不知道从我的Android设备访问声音模式切换器的位置......
任何人都可以提供帮助吗?
答案 0 :(得分:1)
如果您需要通过振动切换到正常模式和静音模式:这是代码:
package com.example.silent.mode.toggle;
import android.os.Bundle;\
import android.app.Activity;
import android.widget.ImageView;
import android.util.Log;
import android.view.View;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.widget.Button;
public class MainActivity extends Activity {
private AudioManager mAudioManager;
private boolean mPhoneIsSilent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
checkIfPhoneIsSilent();
setButtonClickListener();
Log.d("SilentModeApp", "This is a test");
}
private void setButtonClickListener() {
Button toggleButton = (Button)findViewById(R.id.silent);
toggleButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mPhoneIsSilent) {
//change back to normal mode
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
mPhoneIsSilent = false;
}
else
{
//change to silent mode - This Changes to Vibration
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
mPhoneIsSilent = true;
}
// Now toggle the UI again
toggleUi();
}
});
}
private void checkIfPhoneIsSilent() {
int ringermode = mAudioManager.getRingerMode();
if (ringermode == AudioManager.RINGER_MODE_SILENT) {
mPhoneIsSilent = true;
}
else
{
mPhoneIsSilent = false;
}
}
private void toggleUi() {
ImageView imageView = (ImageView) findViewById(R.id.phone_icon);
Drawable newPhoneImage;
if (mPhoneIsSilent){
newPhoneImage =
getResources().getDrawable(R.drawable.phone_silent);
}
else
{
newPhoneImage =
getResources().getDrawable(R.drawable.phone_on);
}
imageView.setImageDrawable(newPhoneImage);
}
@Override
protected void onResume() {
super.onResume();
checkIfPhoneIsSilent();
toggleUi();
}
}
答案 1 :(得分:0)
你可以这样做,声音用充满活力的模式静音,反之亦然
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
if(isSilent){
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}else{
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume, AudioManager.FLAG_PLAY_SOUND);
}
您需要将标记存储在保留值的位置,或者您可以通过此audioManager.getRingerMode()