Android 5.1 wifi 密码掉电后丢失解决

Android 5.1 直接掉电wifi密码解决:

1.一旦wifi连接后,将密码备份

2.在开机的时候将备份密码覆盖wpa_supplicant.conf文件即可

具体实现如下

package com.android.server.wifi;


import java.io.File;
import java.io.FilePermission;


import android.R.bool;
import android.R.integer;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.os.Process;
import android.os.FileUtils;
import android.util.Log;
import com.android.server.SystemService;


public class WifiPasswordBack  extends SystemService{
public final String CONFIG_DIR = "/data/misc/wifi";
public final String WIFI_PWD_BACKUP_FILE = "/data/misc/wpa_supplicant.conf";
public final String WIFI_PWD_CONFIG_FILE = "/data/misc/wifi/wpa_supplicant.conf";


public File backup,config;
Context mContext;
private final String TAG = "wifipwdService";
private Boolean DBG = true;

  public WifiPasswordBack(Context context){
super(context);
mContext = context;
}

@Override
public void onStart() {
if (DBG) {
Log.i(TAG, "wifi password back on start ");
}
Creatfile();
FilePermissionMode(config, 777);
if(RecoverPwdFile()){
FilePermissionMode(config, 660);
}
Thread wifiThread =new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
IntentFilter mFilter = new IntentFilter();
mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
mContext.registerReceiver(wifiReceiver, mFilter);
}
});
wifiThread.start();
}
 
public void Creatfile(){
//if(!backup.exists()){
backup = new File(WIFI_PWD_BACKUP_FILE);
// }
//if (!config.exists()) {
config = new File(WIFI_PWD_CONFIG_FILE);
// }

}

public void FilePermissionMode(File file, int level){

switch (level) {
case 777:
FileUtils.setPermissions(file,
                     FileUtils.S_IRWXU | FileUtils.S_IRWXG |
                     FileUtils.S_IRWXO,
                     Process.myUid(), Process.WIFI_UID);
    if(DBG) Log.i(TAG, "set " + file.getAbsolutePath()+"  777");
break;
case 660:
  FileUtils.setPermissions(file,
                   FileUtils.S_IRUSR | FileUtils.S_IWUSR |
                   FileUtils.S_IRGRP | FileUtils.S_IWGRP,
                   Process.myUid(), Process.WIFI_UID);
    if(DBG) Log.i(TAG, "set " + file.getAbsolutePath()+"  660");
break;
case 644:
FileUtils.setPermissions(file,
                  FileUtils.S_IRUSR | FileUtils.S_IWUSR |
                  FileUtils.S_IRGRP | FileUtils.S_IWGRP |
                  FileUtils.S_IROTH | FileUtils.S_IWOTH,
                  Process.myUid(), Process.WIFI_UID);
       if(DBG) Log.i(TAG, "set " + file.getAbsolutePath()+"  644");
break;


default:
if (DBG) {
Log.e(TAG, "set permission erro");
}
break;
}

}

public boolean BackupPwdFile(){
boolean isCopy = false;
Creatfile();
isCopy = FileUtils.copyFile(config, backup);
return isCopy;
}

public boolean RecoverPwdFile(){
boolean isRcover = false;
Creatfile();
isRcover = FileUtils.copyFile(backup, config);
return isRcover;
}


  private BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (info.getState().equals(NetworkInfo.State.CONNECTED) ||
info.getState().equals(NetworkInfo.State.DISCONNECTED)) {
FilePermissionMode(config, 777);
if (BackupPwdFile()) {
Log.i(TAG, "backup ok ------------");
FilePermissionMode(config, 660);
}

}

}
}
};
}




你可能感兴趣的:(system,Android5.1,wifi密码丢失,备份,system)