首先声明一个Notification, 然后定义一个getSystemService来获得NotificationManager服务对象

Notification noticed = new Notification(); NotificationManager noticedManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); noticedManager.notify(0, noticed);

然后通过点击消息通知跳转到另外一个Activity里,如下:

Intent intent = new Intent(MainActivity.this, NotificationActivity.class); PendingIntent pending = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); Notification noticed = new Notification(); noticed.icon = R.drawable.icon; noticed.tickerText = "状态栏通知"; noticed.defaults = Notification.DEFAULT_SOUND; noticed.setLatestEventInfo(MainActivity.this, "这是一个状态栏通知", "查看", pending); NotificationManager noticedManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); noticedManager.notify(0, noticed);

整个步骤如下:

第一步:

定义一个layout,一个按钮用来单击后出现状态栏通知,

res/layout/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" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/button" android:text="显示消息提示"/> </LinearLayout>

第二步:

由状态通知栏跳转到

res/layout/notification_layout.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:text="启动Notification" android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false" android:layout_gravity="center_horizontal" /> </LinearLayout>

第三步:

src/com/notification/activity/MainActivity.java

package com.notification.activity; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.button); View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, NotificationActivity.class); PendingIntent pending = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); Notification noticed = new Notification(); noticed.icon = R.drawable.icon; noticed.tickerText = "状态栏通知"; noticed.defaults = Notification.DEFAULT_SOUND; noticed.setLatestEventInfo(MainActivity.this, "这是一个状态栏通知", "查看", pending); NotificationManager noticedManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); noticedManager.notify(0, noticed); } }; button.setOnClickListener(listener); } }

第四步:

src/com/notification/activity/NotificationActivity.java

package com.notification.activity; import android.app.Activity; import android.os.Bundle; public class NotificationActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notification_layout); } }

第五步:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.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> <activity android:name=".NotificationActivity" /> </application> <uses-sdk android:minSdkVersion="8" /> </manifest>

效果图:

26. android Notification 状态栏通知_第1张图片

26. android Notification 状态栏通知_第2张图片

更多相关文章

  1. Android解析自定义xml文件(方案一)
  2. android自定义支持横竖方向切换seekbar控件
  3. android spinner控件自定义选择图层
  4. Android WebView自定义处理错误页面显示(404等)
  5. Android AlertDialog 自定义样式-出现-清除黑边白边主题底色 V7
  6. Android Widget添加自定义控件
  7. android:自定义长按/长点击事件
  8. 自定义 ProgressBar 样式

随机推荐

  1. [Android] ContentProvider实例详解
  2. Android NDK工程的编译和链接调试
  3. Android中的小技巧:TextView限制行数和字
  4. android用户界面之ScrollView教程实例汇
  5. Android屏幕解锁图案破解
  6. Android(安卓)图像处理(类型转换,比例缩放,
  7. android gif动画
  8. Android 8.0 新特性
  9. android “设置”里的版本号
  10. Android 测试工具集01