Whenever i enter a Geofence i want to turn my phone silent.The notification should appear too. However,on entering the geofence it just gets silent for 1 second, that is the time while the notification is generated, after the notification is displayed on my screen,it turns back to normal mode automatically. Please help me out on how to permanently turn my phone silent on entering a geofence and similarly to turn it back to normal on leaving the geofence.Here are my GeofenceintentService class->
public class GeofenceIntentService extends IntentService {
private final String TAG = this.getClass().getCanonicalName();
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* //@param name Used to name the worker thread, important only for debugging.
*/
private AudioManager audioManager;
public GeofenceIntentService() {
super("GeofenceIntentService");
}
public void onCreate() {
super.onCreate();
Log.v(TAG, "onCreate GeofenceIntentService");
}
public void onDestroy() {
super.onDestroy();
Log.v(TAG, "onDestroy");
}
@Override
protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
Log.v(TAG, "onHandleIntent");
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if(!geofencingEvent.hasError()) {
int transition = geofencingEvent.getGeofenceTransition();
String notificationTitle;
if(transition==1 || transition==4)
{
audioManager=(AudioManager)getSystemService(AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,6,0);
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE | AudioManager.RINGER_MODE_SILENT);
}
else if(transition==2)
{
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
switch(transition) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
// int flag = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
// ComponentName cName = new ComponentName(GeofenceIntentService.this,RingerModeChangeReceiver.class);
// audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE | AudioManager.RINGER_MODE_SILENT);
// getPackageManager().setComponentEnabledSetting(cName,flag,PackageManager.DONT_KILL_APP);
Log.i("the phone is:", getSystemService(Context.AUDIO_SERVICE) + "");
notificationTitle = "Geofence Entered";
Log.v(TAG, "Geofence Entered");
break;
case Geofence.GEOFENCE_TRANSITION_DWELL:
notificationTitle = "Geofence Dwell";
Log.v(TAG, "Dwelling in Geofence");
break;
case Geofence.GEOFENCE_TRANSITION_EXIT:
notificationTitle = "Geofence Exit";
Log.v(TAG, "Geofence Exited");
break;
default:
notificationTitle = "Geofence Unknown";
}
Log.i("notification", "sent");
sendNotification(this, getTriggeringGeofences(intent), notificationTitle);
}
else
{
Log.i("Geofence ","Error");
}
}
private void sendNotification(Context context, String notificationText,
String notificationTitle) {
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "");
wakeLock.acquire();
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.index)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
.setDefaults(Notification.DEFAULT_ALL).setAutoCancel(false);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
AudioManager audiomanage = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Toast.makeText(getApplicationContext(), "Now in Silent mode", Toast.LENGTH_SHORT).show();
wakeLock.release();
audiomanage.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
private String getTriggeringGeofences(Intent intent) {
GeofencingEvent geofenceEvent = GeofencingEvent.fromIntent(intent);
Log.i("Event is:",geofenceEvent+"");
List<Geofence> geofences = geofenceEvent
.getTriggeringGeofences();
return "Mobile turned silent";
}
}
In your code in the method sendNotification()
you have
audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Toast.makeText(getApplicationContext(), "Now in Silent mode", Toast.LENGTH_SHORT).show();
wakeLock.release();
audiomanage.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
So you set the mode to silent and then straight back to normal within a few lines of code, hence why your solution does not work as you want it to.