android自定义通知栏的效果音

编程入门 行业动态 更新时间:2024-10-27 15:29:56

android<a href=https://www.elefans.com/category/jswz/34/1771438.html style=自定义通知栏的效果音"/>

android自定义通知栏的效果音

在弹出Notification通知的时候有时候想自定义声音,毕竟系统自带的那一种声音太单调了。

本想修改

Notification noti = new Notification(iconid, "通知", System.currentTimeMillis());

noti.defaults = Notification.DEFAULT_SOUND;

在noti进行修改,使其可以播放别的声音,但是找遍了API也没看到相应的方法。

后来索性不播放这个系统自带的效果音了,我调用这个方法的时候播放一个 自定义的通知音就可以了。。

这就是大体的思路,方法很简单。如下:


我的音乐文件放在了raw文件夹下:money.ogg

package com.lxl.testapp;import android.app.Activity;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Color;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.Uri;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;public class MainActivity extends Activity {static Context context;static int NOTIFICATIONS_ID=101;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.button);this.context=this;Button b1=(Button) findViewById(R.id.button1);b1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {showNotification(R.drawable.ic_launcher, MainActivity.class, "积分", "奖励积分奖励积分奖励积分奖励积分奖励积分");}});IntentFilter filter=new IntentFilter();filter.addAction("TTTTT");this.registerReceiver(new MyReceive(), filter);}class MyReceive extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent i) {String package1 = i.getPackage();System.out.println(package1);try {PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);String packageName = info.applicationInfo.packageName;System.out.println(packageName);} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}  Log.e("TEST", context.toString());}}public void showNotification(int iconid,Class c,String title,String content){//建立一个通知实例,第一个参数是图片,第二个标题栏上显示的文字,第三个是时间 Notification noti = new Notification(iconid, "通知", System.currentTimeMillis());//设置跳转的activity,暂时不需要activity的跳转Intent intent=new Intent();//当单击下拉下来的标题内容时候做什么,这里是跳转到主界面。这里和下面是一起的。 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);//设置样式,默认为声音
//       noti.defaults = Notification.DEFAULT_SOUND;//修改提示声音noti.contentView=getRemoteViews("0.15","2.20");noti.contentIntent=contentIntent;
//       Title 是拉下来的标题,Content也是下拉后的内容显示 
//       noti.setLatestEventInfo(context, title, content, contentIntent);//显示这个通知 PhoneManager.getNotificationManager(context).notify(NOTIFICATIONS_ID++, noti);playSound();}//播放自定义的声音public void playSound() {String uri = "android.resource://" + context.getPackageName() + "/"+R.raw.money;Uri no=Uri.parse(uri);Ringtone r = RingtoneManager.getRingtone(getApplicationContext(),no);r.play();}public static RemoteViews getRemoteViews(String reward,String total){RemoteViews contentView = new RemoteViews("com.lxl.testapp",R.layout.noti);contentView.setImageViewResource(R.id.image,R.drawable.money);String text1="恭喜!您获得奖励";String text2="元";String str=text1+reward+text2;SpannableStringBuilder style = new SpannableStringBuilder(str);style.setSpan(new ForegroundColorSpan(Color.parseColor("#ff4e00")), text1.length(),text1.length()+reward.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);contentView.setTextViewText(R.id.title, style);contentView.setTextViewText(R.id.text,"今日累积获得"+total+"元。");return contentView;}
}

更多推荐

android自定义通知栏的效果音

本文发布于:2024-02-25 01:45:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1697376.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   效果   通知   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!