我试图打开便携式Wifi热点ON,通过参考此链接:
如何设置android wifihotspot的高级设置
这在三星Galaxy S3 Android v4.4.2上运行良好.(没有问题)
但是在具有相同或更低Android版本的其他设备上,应用程序崩溃并重新启动设备.
代码如下:
package com.android.startwifi;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class Main extends Activity {
public WifiManager wifiManager;
public Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("hi1","hi");
createWifiAccessPoint();
}
private void createWifiAccessPoint() {
WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
if(wifiManager.isWifiEnabled())
{
wifiManager.setWifiEnabled(false);
}
Method[] wmMethods = wifiManager.getClass().getDeclaredMethods();
boolean methodFound=false;
for(Method method: wmMethods){
if(method.getName().equals("setWifiApEnabled")){
methodFound=true;
WifiConfiguration …
Run Code Online (Sandbox Code Playgroud) 试图创建一个应用程序来下载SD卡上的文件,这是我的代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
public void run() {
Log.i("step0","it starts here");
URLConnection urlConnection = null;
// TODO Auto-generated method stub
try {
//fetching the URL
Log.i("step 1.1","getting the url");
URL url = new URL("http://people.opera.com/howcome/2005/ala/sample.pdf");
Log.i("step 1.2","captured the url");
urlConnection = url.openConnection();
Log.i("step 1.3","captured the url");
urlConnection.connect();
Log.i("step 1","fetching the URL");
//specifying path and file name
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, "filename.pdf"); …
Run Code Online (Sandbox Code Playgroud)