第八章 通知的使用

来源:互联网 发布:线性优化模型 编辑:程序博客网 时间:2024/06/05 15:41

主页面点击一按钮发送通知,通知显示在状态栏上,点开状态栏,跳到通知的页面,并且通知在状态栏消失

public class MainActivity extends ActionBarActivity implements View.OnClickListener {    private Button btnSendNotification;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btnSendNotification = (Button) findViewById(R.id.btn_send_notification);        btnSendNotification.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.btn_send_notification:                NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);                Notification notification = new Notification(R.mipmap.ic_launcher, "This is tickeText", System.currentTimeMillis());                Intent intent = new Intent(MainActivity.this, NotificationActivity.class);                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);                notification.setLatestEventInfo(MainActivity.this, "This is content title", "This is content text", pendingIntent);                manager.notify(1, notification);                break;        }    }}
布局就一个按钮,下面通知的代码,看取消通知,用cancle(id)来取消

public class NotificationActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_notification_text);        NotificationManager manger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        manger.cancel(1);    }}

通知的高级使用

//振动设置long[] vibrate = {0, 1000, 1000, 1000};notification.vibrate = vibrate;//Led灯光提示notification.ledARGB = Color.RED;notification.ledOnMS=1000;notification.ledOffMS = 1000;notification.flags = Notification.FLAG_SHOW_LIGHTS;//声音提示Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/Basic_tone.ogg"));notification.sound = soundUri;//默认提示notification.defaults = Notification.DEFAULT_ALL;

0 0
原创粉丝点击