package?com.example.administrator.notification;
import?android.app.Notification;
import?android.app.NotificationManager;
import?android.app.PendingIntent;
import?android.content.Intent;
import?android.os.Build;
import?android.support.v7.app.ActionBarActivity;
import?android.os.Bundle;
import?android.view.Menu;
import?android.view.MenuItem;
import?android.view.View;
import?android.widget.Button;
import?android.widget.Toast;
public?class?MainActivity?extends?ActionBarActivity?implements?Button.OnClickListener{
????private?Button?btn_send;
????private?Button?btn_cancel;
????private?NotificationManager?manager;
????@Override
????protected?void?onCreate(Bundle?savedInstanceState)?{
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.activity_main);
????????btn_send?=?(Button)findViewById(R.id.btn_send);
????????btn_cancel?=?(Button)findViewById(R.id.btn_cancel);
????????btn_send.setOnClickListener(this);
????????btn_cancel.setOnClickListener(this);
????????manager?=?(NotificationManager)?getSystemService(NOTIFICATION_SERVICE);
????}
????@Override
????public?void?onClick(View?v)?{
????????switch?(v.getId()){
???????????case??R.id.btn_send:
???????????????Intent?intent?=?new?Intent(MainActivity.this,MainActivity.class);
???????????????PendingIntent?pending?=?PendingIntent.getActivity(this,0,intent,0);
???????????????Notification.Builder?build?=?new?Notification.Builder(MainActivity.this);
???????????????build.setSmallIcon(R.mipmap.ic_launcher);
???????????????build.setTicker("i'm?trcker");?//標(biāo)題欄提示?一般和?contentTilte一致
???????????????build.setWhen(System.currentTimeMillis());
???????????????build.setContentTitle("i'm?title");?//通知欄標(biāo)題
???????????????build.setContentText("i'm?text");?//通知欄內(nèi)容
//???????????????build.setContentInfo("i'm?info");
???????????????build.setContentIntent(pending);
???????????????//需要權(quán)限如下
???????????????//////???????????????build.setDefaults(Notification.DEFAULT_SOUND);?//聲音
//???????????????build.setDefaults(Notification.DEFAULT_LIGHTS);?//指示燈
//???????????????build.setDefaults(Notification.DEFAULT_VIBRATE);?//震動
???????????????build.setDefaults(Notification.DEFAULT_ALL);
???????????????Notification?notification?=?build.getNotification();
???????????????manager.notify(110,?notification);
//???????????????Toast.makeText(MainActivity.this,"send",Toast.LENGTH_LONG).show();
???????????????break;
????????????case??R.id.btn_cancel:
????????????????manager.cancel(110);
????????????????break;
????????}
????}
}