ch7_notification.xml:
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 <TextView android:layout_width="fill_parent" 7 android:layout_height="wrap_content" 8 android:text="这是一个Notification使用示例"/> 9 <Button android:id="@+id/bt_sendNotification" 10 android:layout_width="fill_parent" 11 android:layout_height="wrap_content" 12 android:text="发送Notification"/> 13 14</LinearLayout>
ch7_second.xml:
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 <TextView android:layout_width="fill_parent" 7 android:layout_height="wrap_content" 8 android:text="这是一个通过Notification启动的Activity"/> 9 10</LinearLayout>
NotificationActivity.java:
1package com.example.ch7; 2 3import com.example.baseexample.R; 4 5import android.app.Activity; 6import android.app.Notification; 7import android.app.NotificationManager; 8import android.app.PendingIntent; 9import android.content.Intent; 10import android.location.GpsStatus.NmeaListener; 11import android.os.Bundle; 12import android.view.View; 13import android.widget.Button; 14 15public class NotificationActivity extends Activity { 16 17 private Button bt_sendNotification = null; 18 private Intent mIntent = null; 19 private PendingIntent mPendingIntent = null; 20 private Notification mNotification = null; 21 private NotificationManager mNotificationManager = null; 22 23 public void onCreate(Bundle savedInstanceState){ 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.ch7_notification); 26 27 bt_sendNotification = (Button)findViewById(R.id.bt_sendNotification); 28 bt_sendNotification.setOnClickListener(new View.OnClickListener() { 29 30 @Override 31 public void onClick(View v) { 32 mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 33 mIntent = new Intent(NotificationActivity.this,SecondActivity.class); 34 mPendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, mIntent, 0); 35 mNotification = new Notification(); 36 mNotification.icon = R.drawable.ic_launcher; 37 mNotification.tickerText="实例"; 38 mNotification.defaults = Notification.DEFAULT_ALL; 39 mNotification.flags = Notification.FLAG_INSISTENT; 40 mNotification.setLatestEventInfo(NotificationActivity.this, "点击查看", "这是一个Notification示例", mPendingIntent); 41 mNotificationManager.notify(1,mNotification); 42 } 43 }); 44 } 45}
SecondActivity.java :
1package com.example.ch7; 2 3import com.example.baseexample.R; 4 5import android.app.Activity; 6import android.os.Bundle; 7 8public class SecondActivity extends Activity { 9 10 public void onCreate(Bundle savedInstanceState){ 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.ch7_second); 13 } 14}
AndroidManifest.xml 中application节点中添加 :
1 <activity android:name="com.example.ch7.NotificationActivity" android:label="@string/app_name"></activity> 2 <activity android:name="com.example.ch7.SecondActivity" android:label="@string/app_name"></activity>