开发者

Status bar notification with sound doing call

I'm doing an app that need to notify the user doing a call that the call is taking to long... I got every thing up an running and an the notification is made at the right time (I can see it at the status bar) but with no sound. If I make the notification call when there is no call the sound is played.

My notification looks like this:

        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 In开发者_如何学Pythontent(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 notification.audioStreamType using the AudioManager.STREAM_ but with no luck.

Do any body now how to do this? or just an good idea what to try next....


I have an option in my app to play a sound when the user is in a phone call. What you have to do is use the media player to play the sound. Here is my code:

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

Here is the Async Task:

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
    }

}

This has worked well for me so far.


Try using RingtoneManager. The following worked for me: RingtoneManager.getRingtone(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).play();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜