ch7_notification.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是一个Notification使用示例"/>
<Button android:id="@+id/bt_sendNotification"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发送Notification"/>
</LinearLayout>
ch7_second.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是一个通过Notification启动的Activity"/>
</LinearLayout>
NotificationActivity.java:
package com.example.ch7;
import com.example.baseexample.R;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.location.GpsStatus.NmeaListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class NotificationActivity extends Activity {
private Button bt_sendNotification = null;
private Intent mIntent = null;
private PendingIntent mPendingIntent = null;
private Notification mNotification = null;
private NotificationManager mNotificationManager = null;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ch7_notification);
bt_sendNotification = (Button)findViewById(R.id.bt_sendNotification);
bt_sendNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mIntent = new Intent(NotificationActivity.this,SecondActivity.class);
mPendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, mIntent, 0);
mNotification = new Notification();
mNotification.icon = R.drawable.ic_launcher;
mNotification.tickerText="实例";
mNotification.defaults = Notification.DEFAULT_ALL;
mNotification.flags = Notification.FLAG_INSISTENT;
mNotification.setLatestEventInfo(NotificationActivity.this, "点击查看", "这是一个Notification示例", mPendingIntent);
mNotificationManager.notify(1,mNotification);
}
});
}
}
SecondActivity.java :
package com.example.ch7;
import com.example.baseexample.R;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ch7_second);
}
}
AndroidManifest.xml 中application节点中添加 :
<activity android:name="com.example.ch7.NotificationActivity" android:label="@string/app_name"></activity>
<activity android:name="com.example.ch7.SecondActivity" android:label="@string/app_name"></activity>