从ADB配置Wifi

时间:2014-03-21 02:40:38

标签: android permissions adb

我想用Adb配置我的Wifi AP。

我做了一个Java活动:

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiConfiguration wc = new WifiConfiguration();
    wc.SSID = "\"ap_name\"";
    wc.preSharedKey  = "\"password\"";
    wc.hiddenSSID = false;
    wc.status = WifiConfiguration.Status.ENABLED;        

    int res = wifi.addNetwork(wc);
    Log.d("WifiPreference", "add Network returned " + res );
    boolean b = wifi.enableNetwork(res, true);        
    Log.d("WifiPreference", "enableNetwork returned " + b );
    if (b) tv.setText("Internet conectado");
    wifi.saveConfiguration();

在我的宣言中我有:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

当我从Eclipse或安装的应用程序运行它时,它运行正常 但是当我从Adb运行时,用:

adb shell am start -n com.mexico.sgp/.ConfigureWifi

我收到了权限错误:

Starting: Intent { cmp=com.mexico.sgp/.ConfigureWifi }
java.lang.SecurityException: Permission Denial: starting Intent { flg=0x10000000    cmp=com.mexico.sgp/.ConfigureWifi } from null (pid=3279, uid=2000) not exported from uid 10130
at android.os.Parcel.readException(Parcel.java:1425)
at android.os.Parcel.readException(Parcel.java:1379)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1857)
at com.android.commands.am.Am.runStart(Am.java:463)
at com.android.commands.am.Am.run(Am.java:108)
at com.android.commands.am.Am.main(Am.java:81)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:237)
at dalvik.system.NativeStart.main(Native Method)

如何从adb获得许可?

1 个答案:

答案 0 :(得分:1)

在您的清单中,将您的服务标记为exported="true"

<service android:name=".ConfigureWifi"
         android:exported="true" />

根据official docexported控件:

  

其他应用程序的组件是否可以调用服务或与之交互 - &#34; true&#34;如果可以的话,&#34; false&#34;如果不。当值为&#34; false&#34;时,只有相同应用程序的组件或具有相同用户ID的应用程序才能启动该服务或绑定到该服务。

因此将其设置为true允许adb调用您的服务。