[英]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.