结合网上资源和《Google Android SDK开发范例大全》里的5.8,希望把notification里的所有功能都实现在一个项目中。
点击“其它”时,状态栏短暂提示:
接着把状态栏拉下来,可以看到第一个是自定义的notification,第二个是系统的notification
点击第1个会回到NotificationTest.java,我点击第2个,会启动Noti.java
Noti.java
//当user单击Notification留言条时,会运行的Activity public class Noti extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toast.makeText(Noti.this, "我是Noti.java里设的Toast哦", Toast.LENGTH_LONG).show(); finish(); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/white" > <TextView android:id="@+id/mText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="我是TextView,注意:我和下面的Spinner在同一个main.xml文件中哦,只是我比较灰" android:textSize="18sp" /> <Spinner android:id="@+id/mySpinner" android:layout_width="200px" android:layout_height="wrap_content" android:layout_x="30px" android:layout_y="60px" /> </AbsoluteLayout>
myspinner_dropdown.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text1" android:layout_width="fill_parent" android:layout_height="30sp" android:singleLine="true" android:textSize="20sp" style="?android:attr/spinnerDropDownItemStyle"> </TextView>
view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00aaaa"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp"/> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#000"/> <ProgressBar android:id="@+id/pb" android:layout_width="300dip" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:layout_gravity="center_vertical"/> </LinearLayout>
color.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <drawable name="white">#FFFFFFFF</drawable> <drawable name="black">#000000</drawable> </resources>
AndroidManifest.xml(注意要加振动权限,真机的话)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="hyz.com" android:versionCode="1" android:versionName="1.0"> <uses-permission android:name="android.permission.VIBRATE"></uses-permission> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".NotificationTest" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Noti" android:label="@string/app_name"> </activity> </application> </manifest>
NotificationTest.java
package hyz.com; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore.Audio; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.RemoteViews; import android.widget.Spinner; import android.widget.Toast; public class NotificationTest extends Activity { private NotificationManager myNotiManager; private ArrayAdapter<String> myAdapter; private Spinner mySpinner; private static final String[] status = {"在线","离开","忙碌中","马上回来","离线","其它"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myNotiManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); mySpinner = (Spinner)findViewById(R.id.mySpinner); myAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,status); //自定义下拉菜单模式 myAdapter.setDropDownViewResource(R.layout.myspinner_dropdown); mySpinner.setAdapter(myAdapter); mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if(status[position].equals("在线")) { setNotiType(R.drawable.msn,"在线"); } else if(status[position].equals("离开")) { setNotiType(R.drawable.away,"离开"); } else if(status[position].equals("忙碌中")) { setNotiType(R.drawable.busy,"忙碌中"); } else if(status[position].equals("马上回来")) { setNotiType(R.drawable.min,"马上回来"); } else if(status[position].equals("离线")) { setNotiType(R.drawable.offine,"离线"); } else { selfNotiType(); } } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } //发出Notification的method private void setNotiType(int iconId, String text) { Intent notifyIntent = new Intent(this,Noti.class); notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //创建PendingIntent作为设置延运行的Activity PendingIntent appIntent = PendingIntent.getActivity(NotificationTest.this,0, notifyIntent,0); Notification myNoti = new Notification(); myNoti.icon=iconId; //状态栏显示的文字信息 myNoti.tickerText=text; //添加声音 //nofification发生时同时发出默认声音 myNoti.defaults |= Notification.DEFAULT_SOUND; //或使用以下方式: //myNoti.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); //myNoti.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); //如果想要让声音持续重复直到用户对通知做出反应,则可以在notification的flags字段增加"FLAG_INSISTENT" //如果notification的defaults字段包括了"DEFAULT_SOUND"属性,则这个属性将覆盖sound字段中定义的声音 //添加振动 myNoti.defaults |= Notification.DEFAULT_VIBRATE; //自定义振动模式, //long数组可以定义成想要的任何长度 //如果notification的defaults字段包括了"DEFAULT_VIBRATE",则这个属性将覆盖vibrate字段中定义的振动 //long[] vibrate = {0,100,200,300};//0毫秒开始振动,振动100毫秒停止,再过200毫秒振动再次振动300毫秒m //myNoti.vibrate = vibrate; //添加LED灯提醒 myNoti.defaults |= Notification.DEFAULT_LIGHTS; //自定义自己的LED提醒模式 // myNoti.ledARGB = 0xff00ff00;//灯的颜色 // myNoti.ledOffMS = 3000;//亮的时间 // myNoti.ledOnMS = 10000;//灭的时间 myNoti.flags |= Notification.FLAG_SHOW_LIGHTS; //myNoti.flags |= Notification.FLAG_AUTO_CANCEL;//在通知栏上点击此通知后自动清除此通知 //myNoti.flags |= Notification.FLAG_INSISTENT;//重复发出声音,直到用户响应此通知 //myNoti.flags |= Notification.FLAG_ONGOING_EVENT;//将些通知放到通知栏的“正在运行”组中 //myNoti.flags |= Notification.FLAG_NO_CLEAR;//表明在点击了通知栏中的“清除通知后”,些通知不清除 /* //经常与FLAG_ONGOING_EVENT一起使用 * notification.number = 1; //number字段表示此通知代表的当前事件数量,它将覆盖在状态栏图标的顶部 * //如果要使用此字段,必须从1开始 * notification.iconLevel = ; // */ //通知产生的时间,会在通知信息里显示 myNoti.when = System.currentTimeMillis(); //设置notification留言条的参数,appIntent为状态栏里点击产生的事件 myNoti.setLatestEventInfo(NotificationTest.this, "contentTitle", "setLatestEventInfo()中的contentText", appIntent); //送出notification myNotiManager.notify(0, myNoti); } private void selfNotiType() { CharSequence tickerText = "Hello"; Notification myNoti = new Notification(); myNoti=new Notification(R.drawable.other,"我在状态栏里闪了一下,我就是tickerText",System.currentTimeMillis()); //使用自定义的 Notification //1、创建一个自 定义的消息布局 view.xml //2、 在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段 myNoti.contentView = new RemoteViews(getPackageName(),R.layout.view); myNoti.contentView.setImageViewResource(R.id.image, R.drawable.other); myNoti.contentView.setTextViewText(R.id.text, "自定义布局里的TextView"); myNoti.contentView.setProgressBar(R.id.pb, 100, 0, false); //3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要 setLatestEventInfo()方法) Intent notificationIntent = new Intent(this,NotificationTest.class); //主要是设置点击通知时显示内容的类 PendingIntent contentIntent = PendingIntent.getActivity(NotificationTest.this,0,notificationIntent,0); myNoti.contentIntent = contentIntent; //4、发送通知 myNotiManager.notify(1, myNoti); } //当然也可以去除该Notification. @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); myNotiManager.cancelAll(); } }
默认的Notification和自定义的Notification
最新推荐文章于 2020-11-12 21:39:03 发布