|
首先声明一个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"
androidrientation="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>
复制代码
效果图:
|
|