Android学习——状态栏通知
来源:互联网 发布:java获取文件夹的路径 编辑:程序博客网 时间:2024/05/19 01:59
1. 状态栏通知
Notification即通知,用于在通知栏显示提示信息。
在布局文件中添加“创建”和“销毁”两个Button:
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="58dp" android:layout_marginTop="24dp" android:text="创建" android:onClick="create"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="39dp" android:text="销毁" android:onClick="destroy"/>
在java文件中添加单击事件:
public class MainActivity extends Activity { //状态栏通知管理器 private NotificationManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取系统服务——状态栏通知管理者,API 11以下 manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); } public void create(View v) { // 创建通知 Notification notification = new Notification(R.drawable.ic_launcher, "10086欠费提醒", System.currentTimeMillis()); //创建跳转界面 Intent intent = new Intent(this,NewActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); // 设置最新事件,API 11以下 notification.setLatestEventInfo(this, "10086来信", "欠费1毛。。。", pendingIntent); //设置自动退出 notification.flags |= Notification.FLAG_AUTO_CANCEL; //设置默认提示音 notification.defaults |= Notification.DEFAULT_SOUND; //设置震动 notification.defaults |= Notification.DEFAULT_VIBRATE; //设置通知 manager.notify(123, notification); } public void destroy(View v) { manager.cancel(123); }}
在清单文件中添加震动权限:
<uses-permission android:name="android.permission.VIBRATE"/>
API 11及以上版本是通过Notification.Builder类来实现:
public void create(View v) { //动态获取 sdcard的路径 String sdcard_path = Environment.getExternalStorageDirectory().getAbsolutePath(); //uri Uri uri = Uri.parse(sdcard_path+"/aa.mp3"); //创建跳转界面 Intent intent = new Intent(this,NewActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); //位图 Bitmap bm = BitmapFactory.decodeResource(this.getResources(),R.drawable.a); //构造者,静态内部类 Notification.Builder builder = new Notification.Builder(MainActivity.this); //构建链 builder.setAutoCancel(true)//设置是否自动取消 .setWhen(System.currentTimeMillis())//设置什么时候显示通知 .setSmallIcon(R.drawable.ic_launcher)//设置小图标 .setSound(uri) //设置来自sd卡的提示音 .setVibrate(new long[]{300,400,500})//设置震动300ms,隔400ms,再震动500ms .setContentTitle("Title:10086来信") .setContentText("Text:欠费1毛。。。赶紧缴费去") .setContentInfo("Info:10086账单") .setContentIntent(pendingIntent) .setTicker("10086发来贺电"); Notification notification = builder.getNotification();//API11以上,API 16一下// Notification notification = builder.build();//API 16及以上 manager.notify(123, notification); }
运行界面:
点击“弹出2”:
区别:Intent与PendingIntent
Intent立即跳转
PendingIntent延迟跳转
PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
说明:window - - >showview- ->other– >android– > file exploer 查看模拟器文件
sdcard是storage/sdcard的快捷方式
区别:URL与URI
URI:本地或网络地址
URL:网络地址
2. 省略强转
Button button = (Button)findViewById(resId);每次用都需要强制类型转换,如果控件特别多的情况下会很麻烦。现在介绍以下两种解决方法:
方法一:泛型
ViewUtils工具类,通过泛型,省略强转
public class ViewUtils { private Activity activity; public ViewUtils(Activity activity) { this.activity = activity; } public <E extends View> E findView(int resId){ return (E) this.activity.findViewById(resId); }}
调用:
btn = new ViewUtils(this).findView(R.id.btn1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(NewActivity.this, btn.getText().toString(), 0).show(); } });
方法二:注解
butterknife
(1)把jar包放在工程的libs下:
(2)配置是注解生效
右击工程- - >Properties- ->Java Compiler - ->Annotation Processing- ->勾上Enable project specific settings– >Factory Path- ->Enable project specific settings- ->Add JARs- ->当前工程的ibs中的bufferknife-7.0.1.jar
(3)通过注解的方式声明变量:
@Bind(R.id.btn1) Button btn; @Bind(R.id.textView1) TextView tv;
(4) 通过ButterKnife绑定:
ButterKnife.bind(this);//添加监听
- Android学习——状态栏通知
- Android基础—Notification(状态栏通知)详解
- Android 状态栏通知Notification、NotificationManager 学习
- Android通知之状态栏通知
- Notification(状态栏通知)学习
- android 状态栏 通知
- Android 状态栏通知
- Android 状态栏通知Notification
- Android 状态栏通知
- android 发送状态栏通知
- 【Android】状态栏通知Notification
- Android 状态栏通知详解
- android-发送状态栏通知
- android开发 - 状态栏通知
- Android Notification状态栏通知
- 【Android】自定义状态栏通知
- Android监听状态栏通知
- Android 程式开发:(五)发送通知 —— 5.1“状态栏”Notification通知
- Linux内核switch_to宏实现进程切换的原理
- 29. 继承
- java数据库连接池封装及使用
- hdu1071
- Effective Objective-C 2.0 第6条:理解“属性”
- Android学习——状态栏通知
- win7使用eclipse连接hadoop集群,运行mapreduce报错之Failed to set permissions of path
- docker 入门准备step1
- 函数传递const和非const非引用参数的笔记 - 20150905
- 腾讯大数据之新一代资源管理与调度平台
- Word Ladder
- 1028. List Sorting (25)
- hdu 5430 Reflect 求圆镜内反射N次回到出口的方法数 欧拉函数
- C++ 笔试题整理 一