Android 状态栏通知和声音执行调用
我正在做一个应用程序,需要通知正在进行通话的用户,通话时间很长。。。 我把每件事都准备好了,并在正确的时间发出了通知(我可以在状态栏上看到),但没有声音。如果我在没有呼叫时拨打通知电话,则会播放声音 我的通知如下所示:Android 状态栏通知和声音执行调用,android,Android,我正在做一个应用程序,需要通知正在进行通话的用户,通话时间很长。。。 我把每件事都准备好了,并在正确的时间发出了通知(我可以在状态栏上看到),但没有声音。如果我在没有呼叫时拨打通知电话,则会播放声音 我的通知如下所示: NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int icon = R.d
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
}
}
私有静态类playNotificationMediaFileAsyncTask扩展了AsyncTask{
受保护的Void doInBackground(字符串…参数){
MediaPlayer=null;
试一试{
mediaPlayer=新的mediaPlayer();
mediaPlayer.setLooping(false);
setDataSource(_context,Uri.parse(params[0]);
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(新的OnCompletionListener(){
完成时的公共作废(MediaPlayer MediaPlayer){
mediaPlayer.release();
mediaPlayer=null;
}
});
返回null;
}捕获(例外情况除外){
Log.e(“错误:+ex.toString());
mediaPlayer.release();
mediaPlayer=null;
返回null;
}
}
受保护的void onPostExecute(void结果){
//无所事事
}
}
到目前为止,这对我很有效。尝试使用RingtoneManager。以下内容对我有用:
RingtoneManager.getRingtone(上下文,RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知)).play()代码>如果Android在通话过程中自动禁用所有通知声音,我不会感到惊讶;根据我的经验,Android并不真正混合声音(例如,如果你正在听音乐,通知会打断它,而不是在上面混合)。是的,你可能是对的。所以我想没有办法用声音通知打电话的人。