0

I'm android beginner so please be easy on me. I'm doing some "exercises" and i'm writing simple app which will tell RSSI strength of home wifi network. Getting that number is pretty easy, but updating it and showing that on screen it's a little more complicated as i thought.

First this is my onCreate Activity. In this activity i'm launching another android component - Service. Because the code will run in background (i know i could use thread or something else, but this is for "practice" sake, and i have a few ideas what to do with this app, while running service and not interacting with UI )

public class MainActivity extends Activity {
    TextView wifi_check;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        referenceViews();

        startService(new Intent(this, CheckingWifiService.class));
        //wifi_check.setText(""+getIntent().getExtras().getInt("RSSI"));
    }

    private void referenceViews() {

        wifi_check = (TextView) findViewById(R.id.wifiCheck_TV);
    }
}

Because my code will run every second or so, i will use TimerTask for this purpose. And here is my TimerTask class, which includes run() method, and code for executing inside

public class TimerTsk extends TimerTask {
    Context act;
    WifiManager wifiMan;
    WifiInfo info;
    Bundle sendInfo;
    Intent intent;
    int rssi;

    public TimerTsk(Context context) {
        act = context;
    }

    @Override
    public void run() {
        intent = new Intent();  
        sendInfo = new Bundle();

        wifiMan = (WifiManager) act.getSystemService(Activity.WIFI_SERVICE);

        info = wifiMan.getConnectionInfo();
        rssi = info.getRssi();
        Log.d("WORKED", "RUNNING SUCESSFULLY");

        // i want to send info to my activity

        sendInfo.putInt("RSSI", rssi);
        intent.putExtras(sendInfo);
    }
}

From this class , i want to send result of RSSI to my activity and then update a text. But when i call this code below, on activity i always get NullPointerException.

wifi_check.setText(""+getIntent().getExtras().getInt("RSSI"));

To be honest i had hard time figuring out which part of code is throwing an exepction. And i found that more exactly, this part of code is throwing an exepction.

getInt("RSSI")

Overall i see that service is running, because in my LOGCAT i see a message that i create with Log.d in TimerTsk class.

Any ideas why is this happening?

Here is my service class:

public class CheckingWifiService extends Service{

int rssi;
Timer time;
TimerTsk ttsk;




@Override
public IBinder onBind(Intent intent) {

    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {


time = new Timer();
time.schedule(new TimerTsk(getApplicationContext()), 500);





    return START_STICKY;
}

}

Here is my LogCat:

enter image description here

rootpanthera
  • 2,731
  • 10
  • 33
  • 65

2 Answers2

3

I see a common mistake. Don't do this:

sendInfo.putInt("RSSI", rssi);
intent.putExtras(sendInfo); // This adds a Bundle to your existing Bundle!

You are creating an Intent, with a Bundle of extras, with a Bundle that holds rssi. Leave out this unnecessary Bundle:

intent.putExtras("RSSI", rssi);

Now in your next Activity you can use:

getIntent().getIntExtra("RSSI", 0);

However you should always check to make sure there aren't any surprise null variables:

Intent in = getIntent();
if(in != null) {
    int rssi = in.getIntExtra("RSSI", -1);
    if(rssi < 0)
        wifi_check.setText(""+rssi);
    else
        wifi_check.setText("Unknown");
}
Sam
  • 86,580
  • 20
  • 181
  • 179
  • Ok, but I'll need specifics. Post your LogCat errors in your question along with your current code. – Sam Dec 15 '12 at 21:05
  • Oh, next time please highlight the red rows and press Ctrl+C. Then you can paste them into your question so we can see everything and we don't have to squint... :) But I updated my answer. – Sam Dec 15 '12 at 21:25
  • Thanks for your answer.. (sorry about logcat). But code still doesnt seems to work. I'm still getting same logcat error.. – rootpanthera Dec 15 '12 at 21:43
  • I squinted at the LogCat again and noticed that the NPE happens on `MainActivity#onCreate()` specifically line 21. Which line is this? Sorry, line 24, not 21... – Sam Dec 15 '12 at 21:48
  • I had assumed that you were starting a different Activity not MainActivity and you had that code commented out... When MainActivity is first launched `getIntent()` is `null`. I updated my answer. (This assumes that `wifi_check` isn't `null`.) – Sam Dec 15 '12 at 22:16
0

is your activity starting? I don't see any call to startActivity(). In any case as mentioned by Sam you just need to call putExtra for your intent. don't forget to call

is your activity starting? I don't see any call to startActivity(). In any case as mentioned by Sam you just need to call putExtra for your intent. don't forget to call

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

you need to put this flag when start activies from background

Apperside
  • 3,542
  • 2
  • 38
  • 65
  • My activity is starting but only if i don't include that code that crashes the app. This is my starting activity which gets data from timertask and updates it with settext. – rootpanthera Dec 15 '12 at 21:00
  • try simply with intent.putExtra("RSSI", rssi) and getting it with getIntent().getIntExtra("RSSI") – Apperside Dec 15 '12 at 21:17