原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://jackxlee.blog.51cto.com/2493058/682435
Notification和NotificationManager的操作相对比较简单,一般用来获取系统级的服务NotificationManager,然后实例化Notification的对象,设置它的一系列属性(比如说图标、时间、标题、内容等),最后通过NotificationManager发出通知即可。
The description from SDK about Notification:
- A class that represents how a persistent notification is to be presented to the user using the NotificationManager.
- The Notification.Builder has been added to make it easier to construct Notifications.
- For a guide to creating notifications, see the Creating Status Bar Notifications document in the Dev Guide.
接下来使用一个简单的案例来演示一下Notification的使用:
首先MainActivity的代码如下:
- public class MainActivity extends Activity {
- private Button btnSend;
- //定义BroadcastReceiver的action
- private static final String NotificationDemo_Action = "com.ceo.notification.activity.NotificationDemo_Action";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //get the widgets' instance
- getInstance();
- btnSend.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setAction(NotificationDemo_Action);
- sendBroadcast(intent);
- }
- });
- }
- public void getInstance() {
- btnSend = (Button)findViewById(R.id.btnSend);
- }
- }
相对应的main.xml布局:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:textColor="#EEE"
- android:textStyle="bold"
- android:textSize="25sp"
- android:text="Notification应用的小案例"
- />
- <Button
- android:id="@+id/btnSend"
- android:text="send notification"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
SecondActivity的代码如下:
- public class SecondActivity extends Activity {
- private Button btnCancel;
- //声明Notification
- private Notification notification;
- //声明NotificationManager
- private NotificationManager mNotification;
- //标识Notification的ID
- private static final int ID= 1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.second);
- getInstance();
- //怎样获得NotificationManager的实例?
- String service = NOTIFICATION_SERVICE;
- mNotification = (NotificationManager)getSystemService(service);
- //获得Notification的实例
- notification = new Notification();
- //设置该图标 会在状态栏显示
- int icon = notification.icon = android.R.drawable.stat_notify_chat;
- //设置提示信息
- String tickerText = "Test Notification";
- //设置显示时间
- long when = System.currentTimeMillis();
- notification.icon = icon;
- notification.tickerText = tickerText;
- notification.when = when;
- Intent intent = new Intent(this, MainActivity.class);
- PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
- notification.setLatestEventInfo(this, "消息", "Hello Android", pi);
- mNotification.notify(ID, notification);
- btnCancel.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- mNotification.cancel(ID); //--->取消通知
- }
- });
- }
- public void getInstance() {
- btnCancel = (Button)findViewById(R.id.btnCancel);
- }
- }
相对应的second.xml布局:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:textColor="#EEE"
- android:textStyle="bold"
- android:textSize="25sp"
- android:text="显示通知界面"
- />
- <Button
- android:id="@+id/btnCancel"
- android:text="cancel notification"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
MyReceiver的代码如下:
- public class MyReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- //实例化Intent
- Intent i = new Intent();
- //在新任务中启动Activity
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- //设置Intent启动的组件名称
- i.setClass(context, SecondActivity.class);
- //启动Activity,显示通知
- context.startActivity(i);
- }
- }
当然不要忘了在AndroidManifest文件中注册广播和Activity等:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.ceo.notification.activity"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <receiver android:name="MyReceiver">
- <intent-filter>
- <action android:name="com.ceo.notification.activity.NotificationDemo_Action"/>
- </intent-filter>
- </receiver>
- <activity android:name=".SecondActivity"></activity>
- </application>
- <uses-sdk android:minSdkVersion="8" />
- </manifest>
最后直接上图:
至此Android中Notification的使用介绍完毕,预祝大家成功。