package com.halzhang.android.notification;
import android.app.Activity;
import android.os.Bundle;
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;
public class NotificationDemo extends Activity {
/** Called when the activity is first created. */
private static final int NOTICE_ID = 1222;
private Button notify;
private Button cancel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
notify=(Button)findViewById(R.id.noti);
cancel=(Button)findViewById(R.id.cancel);
notify.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
notifyMe();
}
}
);
cancel.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.cancel(NOTICE_ID);
}
});
}
private void notifyMe() {
// TODO Auto-generated method stub
final NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification =new Notification(R.drawable.icon,"通知测试",System.currentTimeMillis());
PendingIntent intent=PendingIntent.getActivity(this, 0, new Intent(this,NotificationDemo.class), 0);
notification.setLatestEventInfo(this, "通知title", "通知信息内容",intent);
manager.notify(NOTICE_ID,notification);
}
}
<?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"
>
<Button android:id="@+id/noti"
android:layout_width="48dip"
android:layout_height="48dip">
</Button>
<Button android:id="@+id/cancel"
android:layout_width="48dip"
android:layout_height="48dip">
</Button>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>