直接上代码,粘贴可用
public class MainActivity extends Activity {
private static final int HELLO_ID=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show();
}
public void show(){
String ns=Context.NOTIFICATION_SERVICE;
NotificationManager no=(NotificationManager)getSystemService(ns);
int icon=R.drawable.ic_launcher;
CharSequence ticker="点我";
long when=System.currentTimeMillis();
Notification notification=new Notification(icon,ticker,when);
Context context=getApplicationContext();
CharSequence contentTitle="别了高山";
CharSequence contentText="你好,别了高山";
Intent notificationIntent =new Intent(this,MainActivity.class);
PendingIntent pIntent=PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, pIntent);
no.notify(HELLO_ID,notification);
}
}