android-状态栏通知有声音正在通话

前端之家收集整理的这篇文章主要介绍了android-状态栏通知有声音正在通话 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在做一个应用,该应用需要通知正在通话的用户通话时间很长…
我正在执行所有操作,并在适当的时间发出了通知(我可以在状态栏上看到它),但是没有声音.如果没有电话时我发出通知电话,则会播放声音.

我的通知如下所示:

        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,notificationIntent,0);

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

    mNotificationManager.notify(2,notification);

我也尝试使用AudioManager.STREAM_来更改notification.audioStreamType,但是没有运气.

请问现在有身体怎么做?或只是个好主意,下一步该怎么做….

最佳答案
我的应用程序中有一个选项,可以在用户通话时播放声音.您要做的就是使用媒体播放器播放声音.这是我的代码

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

这是异步任务:

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
    }

}

到目前为止,这对我来说效果很好.

猜你在找的Android相关文章