Android的状态栏通知(Notification)
2013-06-24 21:48
543 查看
通知用于在状态栏显示消息,消息到来时以图标方式表示,如下:
![](http://img.blog.csdn.net/20130624214007828?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHVlc2h1YW5nc2h1YW5nMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息.
1、Layout布局文件:
2、string.xml
3、MainActivity
注:模拟器要实现震动需要加权限:DEFAULT_VIBRATE
如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息.
1、Layout布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="46dp" android:onClick="test1" android:text="@string/text_notifi" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="94dp" android:onClick="clearNoti" android:text="@string/text_clear" /> </RelativeLayout>
2、string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">lession16-notifi</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="text_notifi">Notification应用案例</string> <string name="text_clear">清除通知</string> </resources>
3、MainActivity
package com.example.lession16_notifi; import android.os.Bundle; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { private NotificationManager notificationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void test1(View v){ showNotification("来短信了", "5557", "I love you", R.drawable.ic_launcher, R.drawable.ic_launcher); } public void showNotification(String tickerText,String contentTitle,String contentText,int iconId,int notiId){ //创建一个Notification Notification notification=new Notification(); //设置通知 消息 图标 notification.icon=iconId; //设置发出消息的内容 notification.tickerText=tickerText; //设置发出通知的时间 notification.when=System.currentTimeMillis(); //设置显示通知时的默认的发声、震动、Light效果 notification.defaults=Notification.DEFAULT_VIBRATE;//震动 //Notification notification = new Notification(R.drawable.ic_launcher, "有新的消息", System.currentTimeMillis()); //3步:PendingIntent android系统负责维护 PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, getIntent(), 0); //4步: 设置更加详细的信息 notification.setLatestEventInfo(this, contentTitle, contentText, pendingIntent); //5步:使用notificationManager对象的notify方法 显示Notification消息 需要制定 Notification的标识 notificationManager.notify(notiId, notification); } public void clearNoti(View v){ notificationManager.cancelAll();//清除所有 } }
注:模拟器要实现震动需要加权限:DEFAULT_VIBRATE
相关文章推荐
- android Notification 状态栏通知使用示例
- 【Android】状态栏通知Notification、NotificationManager详解
- 【Android】状态栏通知Notification、NotificationManager详解
- (转)【Android】状态栏通知Notification、NotificationManager详解
- Android开发 -- 状态栏通知Notification、NotificationManager详解
- 【Android】状态栏通知Notification、NotificationManager详解
- Android 的状态栏通知(Notification)
- 【Android】状态栏通知Notification
- android Notification 状态栏通知使用示例
- 【Android】状态栏通知Notification、NotificationManager详解
- 【Android】状态栏通知Notification、NotificationManager详解
- Android -- 状态栏通知Notification、NotificationManager详解
- android:Notification实现状态栏的通知
- 【Android】状态栏通知Notification、NotificationManager详解
- Android的状态栏通知(Notification)
- Android(Java):Android的状态栏通知(Notification)
- Android状态栏通知(notification)PendingIntent传值(解决传值总是获取第一次的值的问题)
- 【Android】状态栏通知Notification、NotificationManager详解
- Android 状态栏通知Notification、NotificationManager详解
- 【Android开发】消息提示框与对话框-使用Notification在状态栏上显示通知