Notification 通知栏通知的实现
扫描二维码
随时随地手机看文章
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"); //标题栏提示 一般和 contentTilte一致 build.setWhen(System.currentTimeMillis()); build.setContentTitle("i'm title"); //通知栏标题 build.setContentText("i'm text"); //通知栏内容 // build.setContentInfo("i'm info"); build.setContentIntent(pending); //需要权限如下 ////// 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; } } }