1、
package leadingmis.mobile.main;
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.view.View.OnClickListener;
import android.widget.Button;
public class NotificationActivity extends Activity {
private Button notifyBtn;
private Button cancelBtn;
private NotificationManager nm;
private Notification n;
// 通知的ID
public static final int ID = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
notifyBtn = (Button) findViewById(R.id.notify);
cancelBtn = (Button) findViewById(R.id.cancel);
notifyBtn.setOnClickListener(new MyOnClickListener());
cancelBtn.setOnClickListener(new MyOnClickListener());
// 1.获取NotificationManager对象
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 2.初始化Notification对象
n = new Notification();
n.flags = Notification.FLAG_ONGOING_EVENT;
// 设置通知的icon
n.icon = R.drawable.notify;
// 设置通知在状态栏上显示的滚动信息
n.tickerText = "一个通知";
// 设置通知的时间
n.when = System.currentTimeMillis();
}
class MyOnClickListener implements OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.notify:
// 3.设置通知的显示参数
Intent intent = new Intent(NotificationActivity.this, NotificationView.class);
PendingIntent pi = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0);
n.setLatestEventInfo(NotificationActivity.this, "通知标题", "通知内容", pi);
// 4.发送通知
nm.notify(ID, n);
break;
case R.id.cancel:
// 取消通知
nm.cancel(ID);
break;
}
}
}
}
2、
package leadingmis.mobile.main;
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
/**
* 打开通知后跳转的Activity
* @author zuolongsnail
*/
public class NotificationView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notificationview);
// 取消通知
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(NotificationActivity.ID);
}
}
3、
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="sdf"
/>
<Button android:id="@+id/notify" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="发出通知" />
<Button android:id="@+id/cancel" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="取消通知" />
</LinearLayout>
4、
<?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="打开通知后跳转的Activity" />
</LinearLayout>