gpt4 book ai didi

android - 带有通话声音的状态栏通知

转载 作者:太空狗 更新时间:2023-10-29 12:58:52 25 4
gpt4 key购买 nike

我正在开发一个应用程序,需要通知正在通话的用户通话时间过长...我让所有的东西都在运行,并且在正确的时间发出了通知(我可以在状态栏上看到它)但是没有声音。如果我在没有电话时发出通知电话,则会播放声音。

我的通知是这样的:

        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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com