Android之Notification
来源:互联网 发布:php连接数据库语句 编辑:程序博客网 时间:2024/05/30 20:08
activity_main如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.imooc.notification.MainActivity" >
<Button
android:id="@+id/btn_enter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送通知" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消通知" />
</LinearLayout>
MainActivity代码如下:
public class MainActivity extends Activity implements OnClickListener {
private NotificationManager notificationManager; //通知控件类
int nottification_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager = (NotificationManager).getSystemService(Context.NOTIFICATION_SERVICE);
findViewById(R.id.btn_enter).setOnClickListener(this);
findViewById(R.id.btn_cancel).setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_enter:
sendNotification();
break;
case R.id.btn_cancel:
notificationManager.cancel(nottification_id);
break;
}
}
/**
* 构造notification并发送通知
*/
@SuppressLint("NewApi")
private void sendNotification() {
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setTicker("Hello,World!"); // 手机状态栏提示
builder.setWhen(System.currentTimeMillis()); // 设置当前系统时间
builder.setContentTitle("设置通知栏通知"); // 设置消息标题
builder.setContentText("我来自Notification"); // 设置内容
builder.setContentIntent(pendingIntent); // 设置点击后的事件
// builder.setDefaults(Notification.DEFAULT_SOUND); // 设置提示声音
// builder.setDefaults(Notification.DEFAULT_LIGHTS); // 设置提示灯
// builder.setDefaults(Notification.DEFAULT_VIBRATE); // 设置震动
builder.setDefaults(Notification.DEFAULT_ALL); // 设置以上三个
Notification notification = builder.build(); // Android 4.1 以上
// builder.getNotification();
notificationManager.notify(nottification_id, notification);
}
}
- Android 之 Notification
- android之Notification通知
- Android 之 Notification
- android之Notification通知
- Android学习之Notification
- Android之Notification
- android之Notification实现
- Android开发之Notification
- android之Notification通知
- Android之Notification
- Android之基础Notification
- android之Notification通知
- android之Notification通知
- android之Notification通知
- Android之Notification通知
- android之Notification通知
- android之Notification
- android之Notification通知
- Blog One
- 7天入门php-文件上传进度
- AngularJS: $timeout $interval
- Linux系统GNU make
- activity->fragment
- Android之Notification
- Java中实现多线程的两种方式之间的区别
- codeforces 721 Passwords
- 解决sublime在编译出错或警告时额外显示系统path的问题
- DFT,FFT和卷积(笔记)
- lucene入门及安装配置
- 心路杂谈
- Android不同分辨率图片实际显示大小的计算
- iOS开发常用技巧-常见问题篇