Example usage for android.media AudioManager setRingerMode

List of usage examples for android.media AudioManager setRingerMode

Introduction

In this page you can find the example usage for android.media AudioManager setRingerMode.

Prototype

public void setRingerMode(int ringerMode) 

Source Link

Document

Sets the ringer mode.

Usage

From source file:Main.java

public static void setRinger2Normal(Context context) {
    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    //setting the volume
    audioManager.setStreamVolume(AudioManager.STREAM_RING, currentVolume, 0);
    Log.i("HelperFunctions ", "Normal method called");
}

From source file:Main.java

public static void setRingerMode(Context mContext, int x) {
    AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    switch (x) {//from ww w  . ja v  a  2  s  .  co m
    case 0:
        am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        break;
    case 1:
        am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
        break;
    case 2:
        am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        break;
    /*case 3:
       am.setRingerMode(AudioManager.RINGER_MODE_);
       break;*/
    }
}

From source file:com.prey.actions.picture.PictureUtil.java

private static byte[] getPicture(Context ctx, String focus) {
    AudioManager mgr = null;
    SimpleCameraActivity.dataImagen = null;
    int streamType = AudioManager.STREAM_SYSTEM;
    SimpleCameraActivity.activity = null;
    Intent intent = new Intent(ctx, SimpleCameraActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("focus", focus);
    ctx.startActivity(intent);/*w  ww  .j  a  va 2  s  .  c o  m*/
    int i = 0;
    mgr = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
    mgr.setStreamSolo(streamType, true);
    mgr.setRingerMode(AudioManager.RINGER_MODE_SILENT);
    mgr.setStreamMute(streamType, true);
    while (SimpleCameraActivity.activity == null && i < 10) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        i++;
    }
    if (SimpleCameraActivity.activity != null) {
        SimpleCameraActivity.activity.takePicture(ctx, focus);
    }
    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
    }
    mgr.setStreamSolo(streamType, false);
    mgr.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    mgr.setStreamMute(streamType, false);
    try {
        i = 0;
        while (SimpleCameraActivity.activity != null && SimpleCameraActivity.dataImagen == null && i < 5) {
            Thread.sleep(2000);
            i++;
        }
    } catch (InterruptedException e) {
        PreyLogger.i("Error:" + e.getMessage());
    }
    byte[] out = null;
    if (SimpleCameraActivity.activity != null) {
        out = SimpleCameraActivity.dataImagen;
        SimpleCameraActivity.activity.finish();
        SimpleCameraActivity.activity = null;
        SimpleCameraActivity.dataImagen = null;

    }
    return out;
}

From source file:com.intellisol.plugin.Ringer.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (NATIVE_ACTION_STRING.equals(action)) {
        int mode;
        try {/*ww  w  . j ava 2s  .  com*/
            // get mode from argument
            mode = args.getInt(0);

            // get context (Android)
            Context ctxt = cordova.getActivity().getBaseContext();
            if (mode == NORMAL) {
                // set ringer mode
                AudioManager audioManager = (AudioManager) ctxt.getSystemService(Context.AUDIO_SERVICE);
                audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            } else if (mode == VIBRATE) {
                AudioManager audioManager = (AudioManager) ctxt.getSystemService(Context.AUDIO_SERVICE);
                audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
            } else if (mode == SILENT) {
                AudioManager audioManager = (AudioManager) ctxt.getSystemService(Context.AUDIO_SERVICE);
                audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            }

        } catch (JSONException e) {
            // log error
            Log.d("Ringer", e.toString());
            return false;
        } catch (Exception e) {
            // log error
            Log.d("Ringer", e.toString());
            return false;
        }
    }
    return true;
}

From source file:com.metinkale.prayerapp.vakit.AlarmReceiver.java

public static void silenter(Context c, long dur) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
    boolean silent = "silent".equals(prefs.getString("silenterType", "silent"));
    AudioManager aum = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);
    int ringermode = aum.getRingerMode();
    if ((ringermode != AudioManager.RINGER_MODE_SILENT)
            && ((ringermode != AudioManager.RINGER_MODE_VIBRATE) || silent)) {
        AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);

        Intent i;/* w w w . ja va2  s . c o  m*/
        if (ringermode == AudioManager.RINGER_MODE_VIBRATE) {
            i = new Intent(c, setVibrate.class);
        } else {
            i = new Intent(c, setNormal.class);
        }

        PendingIntent service = PendingIntent.getBroadcast(c, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1000 * 60 * dur), service);

        if (PermissionUtils.get(c).pNotPolicy)
            aum.setRingerMode(silent ? AudioManager.RINGER_MODE_SILENT : AudioManager.RINGER_MODE_VIBRATE);

    }
}

From source file:com.godowondev.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received./*  w  ww .j ava 2s.  c  o  m*/
 */
private void sendNotification(String message) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    if (message.startsWith("[ADDITIONAL_MAIL_RING")) {
        intent.putExtra("navi_type", "additional_mail");
        //defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        defaultSoundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.wakeup);

        //sendSMS("01099989584", message);
    } else {
        intent.putExtra("navi_type", "reservation_error");
    }

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.logo).setContentTitle("?? PUSH ").setContentText(message)
            .setAutoCancel(true).setContentIntent(pendingIntent);

    if (message.startsWith("[ADDITIONAL_MAIL_SMS")) {
        //notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
        notificationBuilder.setSound(defaultSoundUri);
    } else if (message.startsWith("[ADDITIONAL_MAIL_RING")) {
        AudioManager audiomanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        audiomanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        notificationBuilder.setSound(defaultSoundUri);
    }

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    Notification note = notificationBuilder.build();

    if ((message.startsWith("[ADDITIONAL_MAIL_SMS_KYS]") || message.startsWith("[ADDITIONAL_MAIL_SMS_ALL]")
            || message.startsWith("[ADDITIONAL_MAIL_RING_KYS]")
            || message.startsWith("[ADDITIONAL_MAIL_RING_ALL]"))
            && getResources().getString(R.string.member_name).equals("?")) {
        notificationManager.notify(0 /* ID of notification */, note);
    }

    if ((message.startsWith("[ADDITIONAL_MAIL_SMS_KDW]") || message.startsWith("[ADDITIONAL_MAIL_SMS_ALL]")
            || message.startsWith("[ADDITIONAL_MAIL_RING_KDW]")
            || message.startsWith("[ADDITIONAL_MAIL_RING_ALL]"))
            && getResources().getString(R.string.member_name).equals("")) {
        notificationManager.notify(0 /* ID of notification */, note);
    }
    //note.flags = Notification.FLAG_INSISTENT; // ?  ? 
    //notificationManager.notify(0 /* ID of notification */, note);
}

From source file:com.z3r0byte.magis.Services.AutoSilentService.java

private void setup() {
    if (autoSilent) {
        TimerTask notificationTask = new TimerTask() {
            @Override/*from  w  w w.j a  v a2  s.  c om*/
            public void run() {
                NotificationManager notificationManager = (NotificationManager) getApplicationContext()
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
                        && !notificationManager.isNotificationPolicyAccessGranted()) {
                    Log.w(TAG, "run: Not allowed to change state of do not disturb!");
                    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                            getApplicationContext());
                    mBuilder.setSmallIcon(R.drawable.magis512);

                    Intent resultIntent = new Intent(
                            android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
                    TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
                    stackBuilder.addParentStack(CalendarActivity.class);
                    stackBuilder.addNextIntent(resultIntent);
                    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                            PendingIntent.FLAG_UPDATE_CURRENT);
                    mBuilder.setContentIntent(resultPendingIntent);

                    mBuilder.setContentTitle("Magis kan de telefoon niet op stil zetten");
                    mBuilder.setContentText("Klik om op te lossen");
                    mBuilder.setAutoCancel(true);
                    mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

                    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                            Context.NOTIFICATION_SERVICE);
                    mNotificationManager.notify(9999, mBuilder.build());
                    return;
                }
                appointments = calendarDB.getSilentAppointments(getMargin());
                if (doSilent(appointments)) {
                    silenced(true);
                    AudioManager audiomanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                    if (audiomanager.getRingerMode() != AudioManager.RINGER_MODE_SILENT) {
                        audiomanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                    }
                } else {
                    if (isSilencedByApp()) {
                        AudioManager audiomanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                        audiomanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                        silenced(false);
                    }
                }
            }
        };
        timer.schedule(notificationTask, 20000, 1000);
    }
}

From source file:com.hari.autotasx.GeofenceTransitionsIntentService.java

@Override
protected void onHandleIntent(Intent intent) {

    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        String errorMessage = GeofenceErrorMessages.getErrorString(this, geofencingEvent.getErrorCode());
        // Log.e(TAG, errorMessage);
        return;//from  w ww .j  a  v a2  s.com
    }

    int geofenceTransition = geofencingEvent.getGeofenceTransition();
    int smsDb = 0;
    int wifiDb = 0;
    int silDb = 0;
    int RemDb = 0;
    int blueDb = 0;
    int carDb = 0;
    String remMsgDb = null;
    int RemDb_carPark = 0;
    String remMsgDb_carPark = null;
    //Creating database object to fetch values
    ManageDB autodb = new ManageDB(this);
    autodb.open();

    if (!CarParkService.car_flag_static) {
        Cursor cursor = autodb.getEntry(ActionFragment.geoFence);
        cursor.moveToFirst();

        //System.out.println("cursor count"+cursor.getCount());

        //Log.i("logcat",cursor.getString(0)+", "+cursor.getString(1)+" , "+cursor.getString(2));
        smsDb = cursor.getInt(5);
        wifiDb = cursor.getInt(6);
        silDb = cursor.getInt(7);
        RemDb = cursor.getInt(8);
        remMsgDb = cursor.getString(9);
        blueDb = cursor.getInt(10);
        carDb = cursor.getInt(11);

        cursor.close();

    } else {
        Cursor cursor_carpark = autodb.getEntryCarPark(CarParkService.geofence_carpark);
        cursor_carpark.moveToFirst();

        carDb = cursor_carpark.getInt(11);
        remMsgDb_carPark = cursor_carpark.getString(9);

        cursor_carpark.close();
    }
    autodb.close();

    //Checking Enter/Exit transitions
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
        String geofenceTransitionDetails = getGeofenceTransitionDetails(this, geofenceTransition,
                triggeringGeofences);
        System.out.println("smsdb" + smsDb);
        if (RemDb == 1) {
            sendNotification(geofenceTransitionDetails, remMsgDb);
        }
        if (carDb == 1) {
            sendNotification(geofenceTransitionDetails, remMsgDb_carPark);
        }
        //Perform actions if set on Actions set
        if (silDb == 1) {
            Toast.makeText(this, "silent", Toast.LENGTH_SHORT).show();
            AudioManager am = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
            am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

        }

        if (wifiDb == 1) {
            WifiManager wifiManager = (WifiManager) getBaseContext().getSystemService(Context.WIFI_SERVICE);
            wifiManager.setWifiEnabled(true);
        }

        if (blueDb == 1) {
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (!mBluetoothAdapter.isEnabled()) {
                mBluetoothAdapter.enable();
            }
        }
        if (smsDb == 1) {
            Toast.makeText(this, "inside sendsms()", Toast.LENGTH_SHORT).show();
            SmsManager sm = SmsManager.getDefault();
            System.out.printf("sm", sm);
            sm.sendTextMessage(ActionFragment.smsNo, null,
                    "Hi, I am in " + ActionFragment.geoFence.getNameLoc() + " now!!!!", null, null);
        }
        if (carDb == 1) {
            Intent mapIntent = new Intent(this, CarParkMap.class);
            mapIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

    } else if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
        String geofenceTransitionDetails = getGeofenceTransitionDetails(this, geofenceTransition,
                triggeringGeofences);
        if (silDb == 1) {
            Toast.makeText(this, "general", Toast.LENGTH_SHORT).show();
            AudioManager am1 = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
            am1.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }

        if (wifiDb == 1) {
            WifiManager wifiManager1 = (WifiManager) getBaseContext().getSystemService(Context.WIFI_SERVICE);
            wifiManager1.setWifiEnabled(false);
        }
        if (blueDb == 1) {
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (mBluetoothAdapter.isEnabled()) {
                mBluetoothAdapter.disable();
            }
        }
        if (smsDb == 1) {
            Toast.makeText(this, "inside sendsms()", Toast.LENGTH_SHORT).show();
            SmsManager sm = SmsManager.getDefault();
            System.out.printf("smsNo :" + ActionFragment.smsNo);
            //fetch
            sm.sendTextMessage(ActionFragment.smsNo, null,
                    "Hi, I am outside " + ActionFragment.geoFence.getNameLoc() + " now !!!", null, null);
        }
        if (RemDb == 1) {
            sendNotification(geofenceTransitionDetails, remMsgDb);
        }

    }

}

From source file:com.xortech.sender.SmsReceiver.java

public void onReceive(final Context ctx, Intent intent) {
    // GET SMS MAP FROM INTENT
    Bundle extras = intent.getExtras();// ww w.  j  a  v  a 2 s  .c o m
    context = ctx;

    // GPS INSTANCE
    gps = new GPSTracker(context);

    // LOAD PREFERENCES
    preferences = PreferenceManager.getDefaultSharedPreferences(context);
    secretCode = preferences.getString("secretCode", DEFAULT_CODE);
    tagID = preferences.getString("tagID", DEFAULT_TAGID);
    senderEnabled = preferences.getBoolean("senderEnabled", true);

    if (extras != null) {

        // GET THE RECEIVED SMS ARRAY
        Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME);

        for (int i = 0; i < smsExtra.length; ++i) {

            // GET THE MESSAGE
            SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);

            // PARSE THE MESSAGE BODY
            String body = sms.getMessageBody().toString();
            String address = sms.getOriginatingAddress();
            long time = System.currentTimeMillis();

            // GET COORDINATES AND SEND A MESSAGE
            gps.getLocation();

            latitude = String.valueOf(Math.round(FIVE_DIGIT * gps.getLatitude()) / FIVE_DIGIT);
            longitude = String.valueOf(Math.round(FIVE_DIGIT * gps.getLongitude()) / FIVE_DIGIT);
            location = "Tag_ID:" + tagID + ":Location:" + latitude + "," + longitude;
            googleString = GOOGLE_STRING + latitude + "," + longitude + "(" + tagID + ")";

            if (body.equals(SECRET_LOCATION_A + secretCode)) {
                if (senderEnabled) {
                    if (latitude.equals("0.0") | longitude.equals("0.0")) {
                        SmsManager.getDefault().sendTextMessage(address, null, NO_COORDS + tagID, null, null);
                    } else {
                        SmsManager.getDefault().sendTextMessage(address, null, googleString, null, null);
                    }
                }

                this.abortBroadcast();
            } else if (body.equals(SECRET_LOCATION_B + secretCode)) {
                if (senderEnabled) {
                    if (latitude.equals("0.0") | longitude.equals("0.0")) {
                        SmsManager.getDefault().sendTextMessage(address, null, NO_COORDS + tagID, null, null);
                    } else {
                        SmsManager.getDefault().sendTextMessage(address, null, location, null, null);
                    }
                }

                this.abortBroadcast();
            } else if (body.contains("Tag_ID:")) {
                // ADD TO DATABASE
                MsgDatabaseHandler dbHandler = new MsgDatabaseHandler(context);

                // VERIFY IF THE TAG EXISTS IN THE ARRAY
                String addressExists = VerifyTagExist(address);

                String[] splitBody = body.split(":");
                String tag = splitBody[1];
                tag.trim();
                String coords = splitBody[3];
                String[] splitCoords = coords.split(",");
                String lat = splitCoords[0];
                lat.trim();
                String lon = splitCoords[1];
                lon.trim();
                String _time = String.valueOf(time);
                String toastMsg = null;

                // CHECK IF THE ADDRESS EXISTS FOR NAMING PURPOSES
                if (addressExists == null) {
                    dbHandler.Add_Message(new MessageData(tag, address, lat, lon, _time));
                    toastMsg = "Response Received: " + tag;
                } else {
                    dbHandler.Add_Message(new MessageData(addressExists, address, lat, lon, _time));
                    toastMsg = "Response Received: " + addressExists;
                }

                dbHandler.close();

                Toast.makeText(context, toastMsg, Toast.LENGTH_LONG).show();

                this.abortBroadcast();
            } else if (body.contains("Panic!")) {

                // OVERRIDE THE SILENT FEATURE
                AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                int max = audio.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
                audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                audio.setStreamVolume(AudioManager.STREAM_RING, max,
                        AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);

                // DEFINE THE NOTIFICATION MANAGER
                notificationManager = (NotificationManager) context
                        .getSystemService(Context.NOTIFICATION_SERVICE);

                // START A TIMER
                mytimer = new Timer(true);

                // SOUND LOCATION ALARM
                soundUri = Uri.parse(BEGIN_PATH + context.getPackageName() + FILE_PATH);

                // DISPLAY TAG ID FOR EMERGENCY
                String[] splitBody = body.split("\n");
                String fieldTag = splitBody[1];
                String[] splitTag = fieldTag.split(":");

                emergencyTag = splitTag[1].trim();

                // TIMER FOR NOTIFICATIONS
                mytask = new TimerTask() {
                    public void run() {
                        // RUN NOTIFICATION ON TIMER
                        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                                .setSmallIcon(R.drawable.emergency).setContentTitle(PANIC_TXT)
                                .setContentText(emergencyTag + UNDER_DURRESS).setSound(soundUri); //This sets the sound to play

                        // DISPLAY THE NOTIFICATION
                        notificationManager.notify(0, mBuilder.build());
                    }
                };
                // START TIMER AFTER 5 SECONDS
                mytimer.schedule(mytask, FIVE_SECONDS);
            }
        }
    }

    // CLEAR THE CACHE ON RECEIVING A MESSAGE
    try {
        MyUpdateReceiver.trimCache(context);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.z3r0byte.magistify.Services.OldBackgroundService.java

private void autoSilentTimer() {
    Log.d(TAG, "autoSilentTimer: Starting autoSilent Timer");
    TimerTask silentTask = new TimerTask() {
        @Override// w  w  w  .  j a v  a 2  s. co  m
        public void run() {
            appointments = calendarDB.getSilentAppointments(getMargin());
            if (doSilent(appointments)) {
                silenced(true);
                AudioManager audiomanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                configUtil.setInteger("previous_silent_state", audiomanager.getRingerMode());
                if (audiomanager.getRingerMode() != AudioManager.RINGER_MODE_SILENT) {
                    audiomanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                }
            } else {
                if (isSilencedByApp()) {
                    AudioManager audiomanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                    if (configUtil.getBoolean("reverse_silent_state")) {
                        audiomanager.setRingerMode(configUtil.getInteger("previous_silent_state"));
                    } else {
                        audiomanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                    }
                    silenced(false);
                }
            }
        }
    };
    timer.schedule(silentTask, 6000, 10 * 1000);
}