如何使用Cordova或Phonegap将电话设置为静音模式? [英] How to set phone to silent mode using Cordova or Phonegap?

查看:55
本文介绍了如何使用Cordova或Phonegap将电话设置为静音模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用我自己的一个在phonegap中的应用程序将android手机设置为静音模式.我该如何实现?

I want to set my android phone to silent mode using one of my own app which is in phonegap. How can I achieve this?

推荐答案

首先在mainActivity类中导入javascriptinterface包

first import javascriptinterface package in mainActivity class

    import android.webkit.JavascriptInterface;

之后,在MainActivity类的onCreate方法中添加JavaScript接口

After that add JavaScript interface in onCreate method in the MainActivity Class

    super.appView.addJavascriptInterface(new JSInterface(), "sampleproj");

然后定义javascript接口

Then define the javascript Interface

    public class JSInterface{
       @JavascriptInterface
       public void gotoSilent(){
           AudioManager audMangr;
           audMangr= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
           audMangr.setRingerMode(AudioManager.RINGER_MODE_SILENT);
       }
    }

在AndroidManifest.xml中添加权限

Add a permission in AndroidManifest.xml

    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

现在在javascript中调用本地方法

Now call the native method in javascript

    window.sampleproj.gotoSilent();

这篇关于如何使用Cordova或Phonegap将电话设置为静音模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆