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, 0, 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 - 带有通话声音的状态栏通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2110934/

相关文章:

android - char*转wchar_t*时如何设置Locale?

java - 如何才能顺利地设置旋转动画?

Android PhotoView 在方向改变后保持缩放

android - 状态栏隐藏 Cordova

Android - 动态添加 fragment

java - 高效更新 RecyclerView 网格或以其他方式显示复杂网格

android - Material Design - 将卡片扩展(过渡)到全屏

android - com.android.dex.DexException : Multiple dex files define Ledu/hawhamburg/vuforia/BuildConfig;

java - 将 java 字符串处理为 Android Studio+NDK 的原生 C/C++

android - 如何将微调器添加到 Activity 的标题栏?