26. android Notification 状态栏通知
首先声明一个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>
效果图:
更多相关文章
- Android解析自定义xml文件(方案一)
- android自定义支持横竖方向切换seekbar控件
- android spinner控件自定义选择图层
- Android WebView自定义处理错误页面显示(404等)
- Android AlertDialog 自定义样式-出现-清除黑边白边主题底色 V7
- Android Widget添加自定义控件
- android:自定义长按/长点击事件
- 自定义 ProgressBar 样式