Android常用零散知識(shí)點(diǎn)

好久沒(méi)有寫過(guò)安卓了阶捆,動(dòng)手又寫了安卓代碼拓春,有些生疏释簿,公司的電腦不讓用USB接入手機(jī),無(wú)奈只能另想辦法調(diào)試硼莽。不過(guò)好在做出了app的雛形辕万,可以實(shí)現(xiàn)監(jiān)控系統(tǒng)的通知,對(duì)關(guān)鍵信息進(jìn)行過(guò)濾沉删,然后再次提醒渐尿。

記一下值得留意的知識(shí)點(diǎn)

Andoird輸出日志到本地存儲(chǔ)

  1. 使用權(quán)限
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  1. 添加日志庫(kù)
    implementation 'de.mindpipe.android:android-logging-log4j:1.0.3'
    implementation 'log4j:log4j:1.2.17'
  1. 配置日志輸出到本地存儲(chǔ)
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,"創(chuàng)建文件失敗" + 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,"發(fā)送了通知:" + builder.toString());

構(gòu)建本地通知

  1. 創(chuàng)建通知的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發(fā)送通知
    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,"發(fā)送了通知:" + builder.toString());
    }

構(gòu)建顯示的對(duì)話框

  1. 添加依賴
implementation 'com.afollestad.material-dialogs:core:0.9.6.0'
  1. 使用,注意傳入的Context是當(dāng)前對(duì)話框所在的Context
MaterialDialog dialog = new MaterialDialog.Builder(ListActivity.this)
                .title("提示")//標(biāo)題
                .content("確認(rèn)要?jiǎng)h除嗎矾瑰?")//內(nèi)容
//                        .icon(getResources().getDrawable(R.mipmap.ic_logo,null))//圖標(biāo)
                .positiveText("確定") //肯定按鍵
//                        .neutralText("稍后詢問(wèn)")  //中性按鍵
                .negativeText("取消") //否定按鍵
                .cancelable(true)
                .onPositive(new MaterialDialog.SingleButtonCallback() { //監(jiān)聽肯定按鍵
                    @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() { //監(jiān)聽否定按鍵
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

                    }
                })
                .build();
        dialog.show();

最后

寫安卓還是有點(diǎn)手生啊砖茸,做出來(lái)的app給別人不是很好用。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末殴穴,一起剝皮案震驚了整個(gè)濱河市凉夯,隨后出現(xiàn)的幾起案子货葬,更是在濱河造成了極大的恐慌,老刑警劉巖劲够,帶你破解...
    沈念sama閱讀 222,590評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件震桶,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡征绎,警方通過(guò)查閱死者的電腦和手機(jī)蹲姐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)人柿,“玉大人柴墩,你說(shuō)我怎么就攤上這事≠灬” “怎么了江咳?”我有些...
    開封第一講書人閱讀 169,301評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)哥放。 經(jīng)常有香客問(wèn)我歼指,道長(zhǎng),這世上最難降的妖魔是什么甥雕? 我笑而不...
    開封第一講書人閱讀 60,078評(píng)論 1 300
  • 正文 為了忘掉前任东臀,我火速辦了婚禮,結(jié)果婚禮上犀农,老公的妹妹穿的比我還像新娘惰赋。我一直安慰自己,他們只是感情好呵哨,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評(píng)論 6 398
  • 文/花漫 我一把揭開白布赁濒。 她就那樣靜靜地躺著,像睡著了一般孟害。 火紅的嫁衣襯著肌膚如雪拒炎。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,682評(píng)論 1 312
  • 那天挨务,我揣著相機(jī)與錄音击你,去河邊找鬼。 笑死谎柄,一個(gè)胖子當(dāng)著我的面吹牛丁侄,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播朝巫,決...
    沈念sama閱讀 41,155評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼鸿摇,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了劈猿?” 一聲冷哼從身側(cè)響起拙吉,我...
    開封第一講書人閱讀 40,098評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤潮孽,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后筷黔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體往史,經(jīng)...
    沈念sama閱讀 46,638評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評(píng)論 3 342
  • 正文 我和宋清朗相戀三年佛舱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了椎例。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,852評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡名眉,死狀恐怖粟矿,靈堂內(nèi)的尸體忽然破棺而出凰棉,到底是詐尸還是另有隱情损拢,我是刑警寧澤,帶...
    沈念sama閱讀 36,520評(píng)論 5 351
  • 正文 年R本政府宣布撒犀,位于F島的核電站福压,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏或舞。R本人自食惡果不足惜荆姆,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評(píng)論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望映凳。 院中可真熱鬧胆筒,春花似錦、人聲如沸诈豌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)矫渔。三九已至彤蔽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間庙洼,已是汗流浹背顿痪。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留油够,地道東北人蚁袭。 一個(gè)月前我還...
    沈念sama閱讀 49,279評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像石咬,于是被迫代替她去往敵國(guó)和親撕阎。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容