package
com.example.test;
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
MainActivity
extends
Activity {
public
static
int
NOTIFYID_1=
1
,NOTIFYID_2=
2
;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
final
NotificationManager notificatiOnManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Button button1=(Button) findViewById(R.id.button1);
button1.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View arg0) {
Notification notify=
new
Notification();
notify.icon=R.drawable.in;
notify.tickerText=
"显示第一个通知"
;
notify.when=System.currentTimeMillis();
notify.defaults=Notification.DEFAULT_ALL;
notify.setLatestEventInfo(MainActivity.
this
,
"无题"
,
"每天进步一点点"
,
null
);
notificationManager.notify(NOTIFYID_1,notify);
Notification notify1=
new
Notification(R.drawable.music,
"显示第二个通知"
,System.currentTimeMillis());
notify1.flags=Notification.FLAG_AUTO_CANCEL;
Intent intent=
new
Intent(MainActivity.
this
,ContentActivity.
class
);
PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.
this
,
0
, intent,
0
);
notify1.setLatestEventInfo(MainActivity.
this
,
"通知"
,
"查看详细内容"
, pendingIntent);
notificationManager.notify(NOTIFYID_2,notify);
}
});
Button button2=(Button) findViewById(R.id.button2);
button2.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View arg0) {
notificationManager.cancel(NOTIFYID_1);
notificationManager.cancelAll();
}
});
}
}