美文网首页Android收藏集
Android常用零散知识点

Android常用零散知识点

作者: Real_man | 来源:发表于2019-08-14 14:55 被阅读4次

    好久没有写过安卓了,动手又写了安卓代码,有些生疏,公司的电脑不让用USB接入手机,无奈只能另想办法调试。不过好在做出了app的雏形,可以实现监控系统的通知,对关键信息进行过滤,然后再次提醒。

    记一下值得留意的知识点

    Andoird输出日志到本地存储

    1. 使用权限
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    1. 添加日志库
        implementation 'de.mindpipe.android:android-logging-log4j:1.0.3'
        implementation 'log4j:log4j:1.2.17'
    
    1. 配置日志输出到本地存储
    public class ALogger {
        public static org.apache.log4j.Logger getLogger(Class clazz) {
            final LogConfigurator logConfigurator = new LogConfigurator();
    //        logConfigurator.setFileName(Environment.getExternalStorageDirectory().toString() + File.separator + "log/monitor_dingding.log");
            logConfigurator.setFileName(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + File.separator + "log/monitor_dingding.txt");
            String fileName = logConfigurator.getFileName();
            File file = new File(fileName);
            if (!file.exists()){
                try {
                    file.createNewFile();
                } catch (IOException e) {
    //                e.printStackTrace();
                    Log.w(MainActivity.TAG,"创建文件失败" + e.getMessage());
                }
            }
            Log.w(MainActivity.TAG,"日志文件名:" + fileName);
            logConfigurator.setRootLevel(Level.ALL);
            logConfigurator.setLevel("org.apache", Level.ALL);
            logConfigurator.setUseFileAppender(true);
            logConfigurator.setFilePattern("%d %-5p [%c{2}]-[%L] %m%n");
            logConfigurator.setMaxFileSize(1024 * 1024 * 5);
            logConfigurator.setImmediateFlush(true);
            logConfigurator.configure();
            Logger log = Logger.getLogger(clazz);
            return log;
        }
    }
    
    1. 写日志
     ALogger.getLogger(getClass()).log(Level.INFO,"发送了通知:" + builder.toString());
    

    构建本地通知

    1. 创建通知的channel
        
         private void createNotificationChannel() {
            // Create the NotificationChannel, but only on API 26+ because
            // the NotificationChannel class is new and not in the support library
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CharSequence name = getString(R.string.channel_name);
                String description = getString(R.string.channel_description);
                int importance = NotificationManager.IMPORTANCE_DEFAULT;
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
                channel.setDescription(description);
                // Register the channel with the system; you can't change the importance
                // or other notification behaviors after this
                NotificationManager notificationManager = getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
            }
        }
    
    1. 使用Channel发送通知
        boolean channelCreated = false;
    
        @OnClick(R.id.sendNotify)
        public void sendNotify(){
            if (!channelCreated){
                createNotificationChannel();
                channelCreated = true;
            }
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("title")
                    .setContentText("text")
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    
    // notificationId is a unique int for each notification that you must define
            notificationManager.notify((int) System.currentTimeMillis() / 1000, builder.build());
            ALogger.getLogger(getClass()).log(Level.INFO,"发送了通知:" + builder.toString());
        }
    

    构建显示的对话框

    1. 添加依赖
    implementation 'com.afollestad.material-dialogs:core:0.9.6.0'
    
    1. 使用,注意传入的Context是当前对话框所在的Context
    MaterialDialog dialog = new MaterialDialog.Builder(ListActivity.this)
                    .title("提示")//标题
                    .content("确认要删除吗?")//内容
    //                        .icon(getResources().getDrawable(R.mipmap.ic_logo,null))//图标
                    .positiveText("确定") //肯定按键
    //                        .neutralText("稍后询问")  //中性按键
                    .negativeText("取消") //否定按键
                    .cancelable(true)
                    .onPositive(new MaterialDialog.SingleButtonCallback() { //监听肯定按键
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            adapter.getData().remove(position);
                            adapter.refreshNotifyItemChanged(position);
    
                            SharedPreferences sp = getSharedPreferences(Constant.SP_NAME, 0);
                            SharedPreferences.Editor spEditor = sp.edit();
                            spEditor.putString(Constant.SP_KEY_FILTER,JSON.toJSONString(adapter.getData()));
                            spEditor.commit();
    
                            Toast.makeText(getApplicationContext(), "成功删除",
                                    Toast.LENGTH_SHORT).show();
                        }
                    })
    
                    .onNegative(new MaterialDialog.SingleButtonCallback() { //监听否定按键
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
    
                        }
                    })
                    .build();
            dialog.show();
    

    最后

    写安卓还是有点手生啊,做出来的app给别人不是很好用。

    相关文章

      网友评论

        本文标题:Android常用零散知识点

        本文链接:https://www.haomeiwen.com/subject/lwbgjctx.html