环信聊天消息提示音的实现 仿微信新消息提示音设置。
思路:用RingtoneManager查询出title,Ringtone,uri信息,title用来展示,Ringtone用来播放,uri设置提示音时保存到本地,消息到来时,从本地获取存储的Uri,然后根据uri获取Ringtone对象,播放提示音。
效果图:
布局文件用ListView
实体类,用来存储提示音信息:
/**
* @author 强
* @time 2017-03-01 10:49
* @类描述:用于存储ringtone对象和uri
* @变更记录:
*/
public class RingtoneBean {
private String title;//提示音标题
private String uriPath;//提示音Uri路径
private Ringtone ringtone;
public RingtoneBean(String title, String uriPath, Ringtone ringtone) {
this.title = title;
this.uriPath = uriPath;
this.ringtone = ringtone;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUriPath() {
return uriPath;
}
public void setUriPath(String uriPath) {
this.uriPath = uriPath;
}
public Ringtone getRingtone() {
return ringtone;
}
public void setRingtone(Ringtone ringtone) {
this.ringtone = ringtone;
}
}
工具类核心方法,获取提示音列表
public List<RingtoneBean> getRingtoneBeanList(int type) {
List<RingtoneBean> resArr = new ArrayList<>();
RingtoneManager manager = new RingtoneManager(mContext);
manager.setType(type);
Cursor cursor = manager.getCursor();
RingtoneBean bean = null;
if (cursor.moveToFirst()) {
do {
//获取uri废了一番功夫,api用的23,好多方法没有了,这句还是查看RingtoneManager源码找到的。以后还是要多看系统源码。
Uri uri = ContentUris.withAppendedId(Uri.parse(cursor.getString(RingtoneManager.URI_COLUMN_INDEX)), cursor
.getLong(RingtoneManager.ID_COLUMN_INDEX));
Log.e("uri",uri.toString());
String title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
Ringtone ringtone = getRingtoneByUriPath(type, uri.toString());
bean = new RingtoneBean(title,uri.toString(),ringtone);
resArr.add(bean);
} while (cursor.moveToNext());
}
return resArr;
}
有了数据,接下来就是适配adapter了
/**
* @author 丁建强
* @time 2017-03-01 13:00
* @类描述:提示音适配器
* @变更记录:
*/
public class SoundAdapter extends BaseAdapter {
private Context mContext;
private int index;//当前选中的index
private List<RingtoneBean> list;
public SoundAdapter(Context mContext, List<RingtoneBean> list) {
this.mContext = mContext;
this.list = list;
}
public void setCurrentIndex(int index) {
this.index = index;
notifyDataSetChanged();
}
@Override
public int getCount() {
return list == null ? 0 : list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if (view == null) {
view = View.inflate(mContext, R.layout.activity_sound_item, null);
holder = new ViewHolder(view);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.tvName.setText(list.get(i).getTitle());
if (index == i) {
holder.iv_sound.setImageResource(R.mipmap.checked);
} else {
holder.iv_sound.setImageResource(R.mipmap.unckecked);
}
return view;
}
static class ViewHolder {
public TextView tvName;
public ImageView iv_sound;
public ViewHolder(View view) {
tvName = (TextView) view.findViewById(R.id.tv_sound_name);
iv_sound = (ImageView) view.findViewById(R.id.iv_sound);
}
}
}
listview点击时,选中当前项,并播放选中的提示音
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
adapter.setCurrentIndex(i);//选中当前项
//停止播放之前的
if (tempRingtone != null && tempRingtone.isPlaying()) {
tempRingtone.stop();
}
tempRingtone = ringtoneBeanList.get(i).getRingtone();
tempRingtone.play();
saveIndex = i;
}
});
点保存按钮时,把选择的提示音的uri保存到本地
tvSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String uriPath = ringtoneBeanList.get(saveIndex).getUriPath();
String title = ringtoneBeanList.get(saveIndex).getTitle();
SharedPreferences.Editor editor = preferences.edit();
if (type == 0) {
editor.putString("private_soundTitle", title);
editor.putString("private_soundUriPath", uriPath);
} else {
editor.putString("group_soundTitle", title);
editor.putString("group_soundUriPath", uriPath);
}
editor.commit();
finish();
}
});
这个功能就差不多了,等环信聊天消息到来时调用环信的EaseNotifier,修改此方法,环信默认的提示音是系统的提示音,可以更改去播放自己设置保存到本地的提示音,自定义提示音就实现了。
/**
* 手机震动和声音提示
*/
public void viberateAndPlayTone(EMMessage message) {
if (message != null) {
if (EMClient.getInstance().chatManager().isSlientMessage(message)) {
return;
}
}
if (System.currentTimeMillis() - lastNotifiyTime < 1000) {
// received new messages within 2 seconds, skip play ringtone
return;
}
try {
lastNotifiyTime = System.currentTimeMillis();
// 判断是否处于静音模式
if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
EMLog.e(TAG, "in slient mode now");
return;
}
EaseSettingsProvider settingsProvider = EaseUI.getInstance().getSettingsProvider();
if (settingsProvider.isMsgVibrateAllowed(message)) {
long[] pattern = new long[]{0, 180, 80, 120};
vibrator.vibrate(pattern, -1);
}
if (settingsProvider.isMsgSoundAllowed(message)) {
if (ringtone == null) {
Uri notificationUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
ringtone = RingtoneManager.getRingtone(appContext, notificationUri);
if (ringtone == null) {
EMLog.d(TAG, "cant find ringtone at:" + notificationUri.getPath());
return;
}
}
if (!ringtone.isPlaying()) {
String vendor = Build.MANUFACTURER;
ringtone.play();
// for samsung S3, we meet a bug that the phone will
// continue ringtone without stop
// so add below special handler to stop it after 3s if
// needed
if (vendor != null && vendor.toLowerCase().contains("samsung")) {
Thread ctlThread = new Thread() {
public void run() {
try {
Thread.sleep(3000);
if (ringtone.isPlaying()) {
ringtone.stop();
}
} catch (Exception e) {
}
}
};
ctlThread.run();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
安卓java代写
java安卓程序代做,安卓程序代做,专业程序代写,有需要请加QQ: 2103752102
我的淘宝店铺
最后
以上就是哭泣小馒头最近收集整理的关于Android仿微信新消息提示音的全部内容,更多相关Android仿微信新消息提示音内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复