1.MainActivity.java中关键代码

Notification notification=new Notification(R.drawable.notee, "this is notification", System.currentTimeMillis()); Intent intent2=new Intent(MainActivity.this,SecondActivity.class); PendingIntent contentIntent=PendingIntent.getActivity(MainActivity.this, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(MainActivity.this, "contentTitle", "contentText", contentIntent); NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(0, notification); 2.manifest.xml配置中添加SecondActivity


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http:///apk/res/android"
package="com.example.yunsheng"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.yunsheng.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>
<service android:name=".ExampleService"></service>
<activity android:name="SecondActivity"></activity>
</application>

</manifest>




3.执行结果



安卓学习:(5)安卓Notification的使用_android