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);//添加监听
0 0