需要帮助...我已经了解了如何制作服务器 - 客户端套接字连接...它的工作原理...现在我想将文件从服务器传输到客户端并返回....这里是我的来源......
套接字服务器......
public class ServerActivity extends Activity {
private TextView serverStatus;
// default ip
public static String SERVERIP = "192.168.1.1";
// designate a port
public static final int SERVERPORT = 8080;
private Handler handler = new Handler();
private ServerSocket serverSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
serverStatus = (TextView) findViewById(R.id.server_status);
SERVERIP = getLocalIpAddress();
Thread fst = new Thread(new ServerThread());
fst.start();
}
public class ServerThread implements Runnable {
public void run() {
try {
if (SERVERIP != …Run Code Online (Sandbox Code Playgroud) 我正在通过以下代码打开便携式wifi热点:
private void createWifiAccessPoint() {
WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
if(wifiManager.isWifiEnabled())
{
wifiManager.setWifiEnabled(false);
}
Method[] wmMethods = wifiManager.getClass().getDeclaredMethods(); //Get all declared methods in WifiManager class
boolean methodFound=false;
for(Method method: wmMethods){
if(method.getName().equals("setWifiApEnabled")){
methodFound=true;
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
try {
boolean apstatus=(Boolean) method.invoke(wifiManager, netConfig,true);
//statusView.setText("Creating a Wi-Fi Network \""+netConfig.SSID+"\"");
for (Method isWifiApEnabledmethod: wmMethods)
{
if(isWifiApEnabledmethod.getName().equals("isWifiApEnabled")){
while(!(Boolean)isWifiApEnabledmethod.invoke(wifiManager)){
};
for(Method method1: wmMethods){
if(method1.getName().equals("getWifiApState")){
int apstate;
apstate=(Integer)method1.invoke(wifiManager);
// netConfig=(WifiConfiguration)method1.invoke(wifi);
//statusView.append("\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n");
}
}
}
}
if(apstatus)
{
System.out.println("SUCCESSdddd"); …Run Code Online (Sandbox Code Playgroud) 我有一个问题,从非活动类读取getSharedPreferences以在播放器中设置播放列表...在我的活动中,我从edittext获取字符串变量来获取文件夹的路径以使用音频文件...
public class MainActivity extends Activity {
String ppp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String PATH = getSharedPreferences("PATH", MODE_PRIVATE).getString("path", ppp);
if (PATH == null){
..........
...........
path_tv.setText("folder is undefined");
}
else {
path_tv.setText("folder defined: /mnt/sdcard/" + PATH);
}
set_path.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (path_edit.getText().toString().length() == 0){
Toast.makeText(getBaseContext(), "folder is undefined", Toast.LENGTH_SHORT).show();
}
else {
ppp = path_edit.getText().toString();
getSharedPreferences("PATH", MODE_PRIVATE)
.edit()
.putString("path", ppp)
.commit();
File folder = new File(Environment.getExternalStorageDirectory() + "/" + ppp); …Run Code Online (Sandbox Code Playgroud)