Status bar message with sound making a call

I am making an application that should notify the user making a call that the call lasts ... Everything works for me, and the notification is made at the right time (I see it in the status bar), but without sound. If I call a notification when there is no call, the sound is played.

My notification is as follows:

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

    int icon = R.drawable.icon;
    CharSequence tickerText = "Hello From CallTimerService";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = getApplicationContext();
    CharSequence contentTitle = "My notification";
    CharSequence contentText = "ss";
    Intent notificationIntent = new Intent(this, CallTimer.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    notification.defaults = Notification.DEFAULT_SOUND;
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    mNotificationManager.notify(2, notification);

I also tried changing the .audioStreamType notification using AudioManager.STREAM_, but no luck.

Now, how to do this, any body? or just a good idea, what to try next ....

+3
source share
2 answers

, , . , . :

if(callStateIdle){
    notification.sound = Uri.parse(notificationSound);
}else{
    new playNotificationMediaFileAsyncTask().execute(notificationSound);                    
}

Async:

private static class playNotificationMediaFileAsyncTask extends AsyncTask<String, Void, Void> {

    protected Void doInBackground(String... params) {
        MediaPlayer mediaPlayer = null;
        try{
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setLooping(false);
            mediaPlayer.setDataSource(_context,  Uri.parse(params[0]));
            mediaPlayer.prepare();
            mediaPlayer.start();
            mediaPlayer.setOnCompletionListener(new OnCompletionListener(){
                public void onCompletion(MediaPlayer mediaPlayer) {
                    mediaPlayer.release();
                    mediaPlayer = null;
                }
            }); 
            return null;
        }catch(Exception ex){
            Log.e("ERROR: " + ex.toString());
            mediaPlayer.release();
            mediaPlayer = null;
            return null;
        }
    }

    protected void onPostExecute(Void result) {
        //Do Nothing
    }

}

.

+2

RingtoneManager. : RingtoneManager.getRingtone(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).play();

+1

Source: https://habr.com/ru/post/1729415/


All Articles