I'm trying to write a program in which user can set time that will change ringer mode to vibrate or normal in background. When I set the start time it's working but at the end time it's not changing and device continuing vibrate mode. I just want, at the start time phone would be in vibrate and at end time phone would go back into the silent mode.
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);
}}
and these are related BroadcastReceiver class..
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;
}
}}
and I declared
<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>
in my manifest file.
Please help me to solve this issue. Thanks.
Dont't use both the receiver (MyReceiver and Myreceiver1) use single MyReceiver.
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.media.RINGER_MODE_CHANGED" />
</intent-filter>
</receiver>
来源:https://stackoverflow.com/questions/27893727/how-to-change-ringer-mode-in-backgroundvibrate-or-silent