1

Hello im learning android and need some help. I want to make app witch reads your wifi ip but i getting error please help me.

MainActivity.

public class MainActivity extends ActionBarActivity {

    MyService karolisService;
    boolean isBound = false;

    public void showIP(View view){
        String showIP = karolisService.getIpAddr();
        TextView karolisText = (TextView) findViewById(R.id.textView);
        karolisText.setText(showIP);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent i = new Intent(this, MyService.class);
        bindService(i, karolisConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private ServiceConnection karolisConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MyBinder binder = (MyBinder) service;
            karolisService = binder.getService();
            isBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isBound = false;
        }
    };

MyService
public class MyService extends Service {

    private final IBinder karolisBinder = new MyBinder();

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return karolisBinder;
    }

    public String getIpAddr(){
        WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
        String showIP = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
        return Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
    }

    public class MyBinder extends Binder {
        MyService getService(){
            return MyService.this;
        }
    }

}

Android manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.karolis.myapk" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" ></uses-permission>
        <user-permission android:name="android.permission.INTERNET" />
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true" >
        </service>
    </application>

</manifest>

logcat

    4-26 17:35:49.507  13805-13805/com.example.karolis.myapk D/AndroidRuntime﹕ Shutting down VM
    04-26 17:35:49.507  13805-13805/com.example.karolis.myapk E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.example.karolis.myapk, PID: 13805
        java.lang.IllegalStateException: Could not execute method of the activity
                at android.view.View$1.onClick(View.java:4247)
                at android.view.View.performClick(View.java:5191)
                at android.view.View$PerformClick.run(View.java:21019)
                at android.os.Handler.handleCallback(Handler.java:739)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:5940)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
         Caused by: java.lang.reflect.InvocationTargetException
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at android.view.View$1.onClick(View.java:4242)
                at android.view.View.performClick(View.java:5191)
                at android.view.View$PerformClick.run(View.java:21019)
                at android.os.Handler.handleCallback(Handler.java:739)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:5940)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
         Caused by: java.lang.SecurityException: WifiService: Neither user 10257 nor current process has android.permission.ACCESS_WIFI_STATE.
                at android.os.Parcel.readException(Parcel.java:1540)
                at android.os.Parcel.readException(Parcel.java:1493)
                at android.net.wifi.IWifiManager$Stub$Proxy.getConnectionInfo(IWifiManager.java:1348)
                at android.net.wifi.WifiManager.getConnectionInfo(WifiManager.java:1485)
                at com.example.karolis.myapk.MyService.getIpAddr(MyService.java:35)
                at com.example.karolis.myapk.MainActivity.showIP(MainActivity.java:29)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at android.view.View$1.onClick(View.java:4242)
                at android.view.View.performClick(View.java:5191)
                at android.view.View$PerformClick.run(View.java:21019)
                at android.os.Handler.handleCallback(Handler.java:739)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:5940)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
    04-26 17:35:51.977  13805-13805/com.example.karolis.myapk I/Process﹕ Sending signal. PID: 13805 SIG: 9
4
  • as in Log Neither user 10257 nor current process has android.permission.ACCESS_WIFI_STATE. Commented Apr 26, 2015 at 15:02
  • <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" ></uses-permission> sorry havent posted manifest, but permissions added Commented Apr 26, 2015 at 15:04
  • move permissions outside <application> tag Commented Apr 26, 2015 at 15:11
  • got it thank you. one more question, any ideas why it formating wrong ip? last numbers i get 64, when my ip ends 254. Commented Apr 26, 2015 at 15:28

2 Answers 2

2

Add permissions in your Manifest.xml before application tag.

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
     <user-permission android:name="android.permission.INTERNET" /> 

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.karolis.myapk" >

 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
     <user-permission android:name="android.permission.INTERNET" /> 

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true" >
        </service>
    </application>

</manifest>
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, i feel so noob now. As i said learning, thank you for you help :))
0
<user-permission android:name="android.permission.INTERNET"/>

should be

<uses-permission android:name="android.permission.INTERNET"/>

That worked for me. I also added to give access to write in Downloads/ folder

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.