好久沒(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ǔ)
- 使用權(quán)限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- 添加日志庫(kù)
implementation 'de.mindpipe.android:android-logging-log4j:1.0.3'
implementation 'log4j:log4j:1.2.17'
- 配置日志輸出到本地存儲(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;
}
}
- 寫日志
ALogger.getLogger(getClass()).log(Level.INFO,"發(fā)送了通知:" + builder.toString());
構(gòu)建本地通知
- 創(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);
}
}
- 使用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ì)話框
- 添加依賴
implementation 'com.afollestad.material-dialogs:core:0.9.6.0'
- 使用,注意傳入的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給別人不是很好用。