編輯:關於Android編程
在網上查了一些資料。U盤檢測的方法一般有兩種方式,檢測電池或者查看配置信息。
1.Batterymanager檢測方法如下:
[java]
IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(BtStatusReceiver, mIntentFilter);
public BroadcastReceiver BtStatusReceiver = new BroadcastReceiver() // receive broadcast that BT Adapter status change
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED))
{
Log.d("Battery", "" + intent.getIntExtra("plugged", 0));
Toast text=Toast.makeText(context, "ACTION_USB_DEVICE_ATTACHED"+intent.getIntExtra("plugged", 0), Toast.LENGTH_LONG);
text.show();
dataview.setText(""+intent.getIntExtra("plugged", 0));
}
}
};
IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(BtStatusReceiver, mIntentFilter);
public BroadcastReceiver BtStatusReceiver = new BroadcastReceiver() // receive broadcast that BT Adapter status change
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED))
{
Log.d("Battery", "" + intent.getIntExtra("plugged", 0));
Toast text=Toast.makeText(context, "ACTION_USB_DEVICE_ATTACHED"+intent.getIntExtra("plugged", 0), Toast.LENGTH_LONG);
text.show();
dataview.setText(""+intent.getIntExtra("plugged", 0));
}
}
};
intent.getIntExtra("plugged", 0)得到的值分別為:
0):斷開
1):連上USB
2):連上了充電器
2檢測listroot是否有文件路徑多出
[java]
public class testU implements Runnable{
private File[] roots=File.listRoots();
public testU() {
}
public void run() {
System.out.println("檢測系統開啟...");
while (true) {
File[] tempFile = File.listRoots();
boolean sign = false;
if (tempFile.length > roots.length) {
for (int i = tempFile.length -1; i >= 0; i--) {
sign = false;
for(int j = roots.length -1; j >= 0; j--) {
if(tempFile[i].equals(roots[j])) {
sign = true;
}
}
if (sign == false) {
System.out.println("插入盤符:"+tempFile[i].toString());
}
}
roots=File.listRoots();//更新roots
} else {
for (int i = roots.length - 1;i >= 0; i--) {
sign = false;
for(int j = tempFile.length- 1; j >= 0; j--) {
if(tempFile[j].equals(roots[i])) {
sign = true;
}
}
if (sign == false) {
System.out.println("退出盤符:"+roots[i].toString());
}
}
roots=File.listRoots();//更新roots
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(testU.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String args[]) {
new Thread(new testU()).start();
}
}
public class testU implements Runnable{
private File[] roots=File.listRoots();
public testU() {
}
public void run() {
System.out.println("檢測系統開啟...");
while (true) {
File[] tempFile = File.listRoots();
boolean sign = false;
if (tempFile.length > roots.length) {
for (int i = tempFile.length -1; i >= 0; i--) {
sign = false;
for(int j = roots.length -1; j >= 0; j--) {
if(tempFile[i].equals(roots[j])) {
sign = true;
}
}
if (sign == false) {
System.out.println("插入盤符:"+tempFile[i].toString());
}
}
roots=File.listRoots();//更新roots
} else {
for (int i = roots.length - 1;i >= 0; i--) {
sign = false;
for(int j = tempFile.length- 1; j >= 0; j--) {
if(tempFile[j].equals(roots[i])) {
sign = true;
}
}
if (sign == false) {
System.out.println("退出盤符:"+roots[i].toString());
}
}
roots=File.listRoots();//更新roots
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(testU.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String args[]) {
new Thread(new testU()).start();
}
}
檢測u盤插入後,獲取u盤總內存及可利用內存可以采用statFs獲取。類似SD卡。
final StatFs stat = new StatFs(Udisk.getPath());
final long blockSize = stat.getBlockSize();
final long totalBlocks = stat.getBlockCount();
final long availableBlocks = stat.getAvailableBlocks();
long mTotalSize = totalBlocks * blockSize;
long mAvailSize =availableBlocks * blockSize;
1、list(列表) 列表是常用的UI控件,mui封裝的列表組件比較簡單,只需要在ul節點上添加.mui-table-view類、在li節點上添加.mui-table-
關鍵詞:bluedroid bluez作者:xubin341719(歡迎轉載,請注明作者,請尊重版權,謝謝!)歡迎指正錯誤,共同學習、共同進步!!一、名詞解釋:(有用信息
用android studio創建文件隨便寫了個demo,項目文件就90多M,感覺沒干什麼就導致文件這麼大了,瞬間頭皮發麻。。。為什麼呢?帶著疑惑,打開android s
已經提過事件在分發前要做攔截的事情,只不過當時沒有展開來分析,因此這篇文章的主要目的就是分析事件在分發前的攔截過程。(注:Android源碼版本為6.0)我們分析到Inp