import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListener();
}
private void setListener() {
findViewById(R.id.btnSendNotification).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//创建启动目标Activity的Intent对象
Intent intent=new Intent(MainActivity.this, SecondActivity.class);
//创建PendingIntent对象
PendingIntent pi=PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
//创建Notification对象
Notification notif=new Notification(R.drawable.ic_launcher, "通知来啦!", System.currentTimeMillis());
//设置点击通知的监听
notif.setLatestEventInfo(MainActivity.this, "您右16款软件可以更新", "点击开始更新", pi);
//创建通知管理器对象
NotificationManager manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//发送通知
manager.notify(99, notif);
// manager.cancel(99);
}
});
}
}