我试图编写一个程序,用户可以在其中设置将振铃模式改变为背景振动或正常的时间。当我设定开始时间时,它会工作但在结束时它没有改变,设备继续振动模式。 我只想要,在开始时手机会振动,在结束时手机会重新进入静音模式。
MainActivity.java
public class MainActivity extends Activity {
private PendingIntent pendingIntent;
private AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
Calendar calendar1 = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 4);
calendar.set(Calendar.MINUTE, 37);
calendar.set(Calendar.AM_PM,Calendar.AM);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, (int)System.currentTimeMillis(), myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
calendar1.set(Calendar.HOUR_OF_DAY, 4);
calendar1.set(Calendar.MINUTE, 38);
calendar1.set(Calendar.AM_PM,Calendar.AM);
Intent myIntent1 = new Intent(MainActivity.this, MyReceiver1.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, (int)System.currentTimeMillis(), myIntent1,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), pendingIntent);
}}
这些是相关的BroadcastReceiver类..
MyReceiver.java
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
AudioManager audio = (AudioManager)arg0.getSystemService(Context.AUDIO_SERVICE);
switch(audio.getRingerMode()){
case AudioManager.RINGER_MODE_NORMAL :
audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
Log.d("MODE", "was normal");
Log.d("MODE", "is vibrate");
break;
case AudioManager.RINGER_MODE_SILENT :
audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
Log.d("MODE", "was silent");
Log.d("MODE", "is vibrate");
break;
}
}}
MyReceiver1.java
public class MyReceiver1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
AudioManager audio=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
// TODO Auto-generated method stub
switch(audio.getRingerMode()){
case AudioManager.RINGER_MODE_NORMAL :
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Log.d("MODE", "was normal");
Log.d("MODE", "is silent");
break;
}
case AudioManager.RINGER_MODE_VIBRATE :
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Log.d("MODE", "was vibrate");
Log.d("MODE", "is silent");
break;
}
}}
我宣布了
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.media.RINGER_MODE_CHANGED" />
</intent-filter>
</receiver>
<receiver android:name=".MyReceiver1">
<intent-filter>
<action android:name="android.media.RINGER_MODE_CHANGED" />
</intent-filter>
</receiver>
在我的清单文件中。
请帮我解决这个问题。 感谢。
答案 0 :(得分:0)
不要同时使用接收器(MyReceiver和Myreceiver1)使用单个MyReceiver。
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.media.RINGER_MODE_CHANGED" />
</intent-filter>
</receiver>
&#13;