package com.example.mynotifycation;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Notification.Builder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* Notifycation 使用总结
* @Describe:
* @package: com.example.mynotifycation
* @author shaobn
* @date 2015-9-14 下午3:32:30
*/
public class MainActivity extends Activity {
private Button button;
private NotificationManager notificationManager;
private Notification.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button1);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
builder = new Notification.Builder(MainActivity.this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,MainActivity.class);
//PendingIntent是Intent的一种封装,并不是立刻执行intent,只有在响应pendingIntent后才执行intent
PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 1);
builder.setContentIntent(pIntent);
builder.setContentTitle("notify");
builder.setContentText("hello world");
//必须要有设置图片,否则出不来
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setTicker("通知来了");
// builder.setDefaults(Notification.DEFAULT_VIBRATE);
//停100ms,震动250ms,再停1000ms,再震动500ms,注意需要在配置文件中声明震动权限 android.permission.VIBRATE
long [] ll = {100, 250, 1000, 500};
builder.setVibrate(ll);
Notification notification = builder.build();
notificationManager.notify(100, notification);
}
});
}
}