Notification01.java:
package com.Notification01;
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;
import android.widget.TextView;
public class Notification01 extends Activity {
TextView mTextView;
Button mButton01,mButton02,mButton03,mButton04;
Notification mNotification;
NotificationManager mNotificationManager;
Intent mIntent;
PendingIntent mPendingIntent;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton01=(Button)findViewById(R.id.Button01);
mButton02=(Button)findViewById(R.id.Button02);
mButton03=(Button)findViewById(R.id.Button03);
mButton04=(Button)findViewById(R.id.Button04);
//初始化NotificationManager对象
mNotificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//构造Notification对象
mNotification=new Notification();
//点击通知时转移内容
mIntent=new Intent(Notification01.this,Activity02.class);
//主要是设置点击通知时显示内容的类
mPendingIntent=PendingIntent.getActivity(Notification01.this, 0, mIntent, 0);
mButton01.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
//设置通知在状态栏显示的图标
mNotification.icon=R.drawable.img1;
//当我们点击通知时显示的内容
mNotification.tickerText="Button1通知内容。。。。。";
//通知时发出默认的声音
mNotification.defaults=Notification.DEFAULT_SOUND;
//设置通知显示的参数
mNotification.setLatestEventInfo(Notification01.this, "mButton01", "mButton01通知", mPendingIntent);
//可以理解为执行这个通知
mNotificationManager.notify(0,mNotification);
}
});
mButton02.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
mNotification.icon = R.drawable.img2;
mNotification.tickerText = "Button2通知内容...........";
//通知时震动
mNotification.defaults = Notification.DEFAULT_VIBRATE;
mNotification.setLatestEventInfo(Notification01.this, "Button2", "Button2通知", mPendingIntent);
mNotificationManager.notify(0, mNotification);
}
});
mButton03.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
mNotification.icon = R.drawable.img3;
mNotification.tickerText = "Button3通知内容...........";
//通知时屏幕发亮
mNotification.defaults = Notification.DEFAULT_LIGHTS;
mNotification.setLatestEventInfo(Notification01.this, "Button3", "Button3通知", mPendingIntent);
mNotificationManager.notify(0, mNotification);
}
});
mButton04.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
mNotification.icon = R.drawable.img4;
mNotification.tickerText = "Button4通知内容..........";
//通知时既震动又屏幕发亮还有默认的声音
mNotification.defaults = Notification.DEFAULT_ALL;
mNotification.setLatestEventInfo(Notification01.this, "Button4", "Button4通知", mPendingIntent);
mNotificationManager.notify(0, mNotification);
}
});
}
}
Activity02.java:
package com.Notification01;
import android.app.Activity;
import android.os.Bundle;
public class Activity02 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//这里直接限制一个TextView
setContentView(R.layout.main2);
}
}
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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="4dip"
/>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button01"
>
<requestFocus/>
</Button>
<Button
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button02"
>
</Button>
<Button
android:id="@+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button03"
>
</Button>
<Button
android:id="@+id/Button04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button04"
>
</Button>
</LinearLayout>
main2.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="谢谢使用!"
/>
</LinearLayout>