简体   繁体   English

将我的手机设置为静音模式

[英]Set my mobile phone to silent mode

I have a widget and i am trying to set my phone volume silent mode without using Activity 我有一个小部件,我尝试不使用“ Activity来设置电话音量静音模式

My widget class : 我的widget类:

   public class MyWidget extends AppWidgetProvider {



  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
      int[] appWidgetIds) {


    ComponentName thisWidget = new ComponentName(context,
        MyWidget.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
    for (int widgetId : allWidgetIds) {



      RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
          R.layout.widget_layout);

      Intent i = new Intent(context,mutee.class);
      PendingIntent i1 = PendingIntent.getActivity(context, 0, i, 0);
      remoteViews.setOnClickPendingIntent(R.id.imageButton4silent, i1);

    appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
  }
}

Now i want to use mutee class and set my phone volume silent I actually tried this but it doesnt work : 现在我想使用静音class ,并将电话音量设置为silent我实际上尝试过此操作,但它不起作用:

public class mutee  {

    private Context context;    


     mutee(Context context){
        this.context=context;
    }

    private void setRingerMode (int ringerMode){

        AudioManager e = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        //ringerMode=0x00000002;//normal
        e.setRingerMode(ringerMode);


    }

     mutee() {
         setRingerMode(0x00000002);
    }

}

You can hold your application context in a reference which you can pass on to your helper class easily. 您可以将应用程序上下文保存在一个引用中,该引用可以轻松传递给您的帮助器类。 In your context you need to pass that reference to your Mute.class 在您的上下文中,您需要将该引用传递给您的Mute.class

Code Snippet to Hold your application reference throught out the Application:- 用于在整个应用程序中保留您的应用程序引用的代码段:-

public class YourApplication extends Application{
  static YourApplication yourapplication;

  public void onCreate() {
            super.onCreate();
            yourapplication = this;
   }
   public static YourApplication getApplication()
    {
            return yourapplication;
    }
  public void onTerminate() {
            // TODO Auto-generated method stub
            super.onTerminate();
    }
}

Now as yourapplication hold the context of your application, 现在,作为yourapplication握住你的应用程序的情况下,

You can call getSystemService() on it. 您可以在其上调用getSystemService()

Code Snippet:- 代码段:-

 AudioManager am;
 am= (AudioManager)yourapplication.getSystemService(Context.AUDIO_SERVICE);
 am.setRingerMode(AudioManager.RINGER_MODE_SILENT);

You need to call YourApplication extends Application from your Manifest file 您需要从清单文件中调用YourApplication扩展Application

<application 
android:label="@string/app_name"
...
android:name="com.abc.demo.YourApplication"
...>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Android 7(API 级别 24)中,我的应用程序不允许将电话静音(将铃声模式设置为静音) - In Android 7 (API level 24) my app is not allowed to mute phone (set ringer mode to silent) 虽然设置了手机静音模式,但在Android通知中播放声音 - Play sound in Android notifications although phone silent mode is set 如何将铃声模式完全设置为静音? - How to completely set ringer mode to silent? 如何设置安静模式Android的时间 - How to set time for silent mode Android 如何将设备设置为静音模式而不振动 - How to set device to silent mode without vibration Android:如何在Lollipop中将响铃模式设置为静音 - Android : How to set ringing mode to silent in Lollipop 将Xamarin Android中的BroadCast接收过程中的Chang手机更改为静音模式 - Chang Phone to Silent Mode on BroadCast Recieving in Xamarin Android Android:铃声模式改为静音不会阻止手机振动 - Android: Ringer mode changed to silent doesn't stop the phone vibrating 如何以编程方式检测 android 手机是否处于静音模式 - how can I detect whether the android phone in Silent mode programmatically 如何在某些特定来电号码上将手机状态更改为静音模式? - How to change state of phone to silent mode on some particular incoming number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM