3

I am new to Android development and managed to get this far reading questions and answers on StackOverflow. Thank you everyone.

I came up with a problem that I just can't figure out. I have seen similar problems posted but the answers are not clear to me. Please help me on this one.

I want to call a method on my main activity from another class. The app crashes when I try to call the method. Here is the code:

On the class file:

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        MainActivity MainActivity = new MainActivity();
        MainActivity.ligaInternet();
    }
}

On the mainactivity file:

protected void ligaInternet() {
    ConnectivityManager connMgr = (ConnectivityManager)
          getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        String urlText="http://www.regiprof.com/regiprof_sms.php";
        String stringUrl = urlText.toString();
        new DownloadWebpageTask().execute(stringUrl);
    } 
}

How can I call the ligaInternet() function?

Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67

3 Answers3

1

you can try

MainActivity currentActivity = ((MainActivity)context.getApplicationContext()).getCurrentActivity();
currentActivity.ligaInternet();
Angel Koh
  • 12,479
  • 7
  • 64
  • 91
  • Angel Koh, Tried you sugestion and get an error on "getCurrentActivity()" saying: canot resolve method. Any further ideas? Thank you in advance. – Miguel Fernandes Aug 25 '15 at 20:30
  • Sorry I don't understand where to include the example im my code. Could you please show me using my code snipet? – Miguel Fernandes Aug 26 '15 at 00:52
  • it seems that someone else already have a solution for your problem http://stackoverflow.com/questions/6468463/start-activity-inside-onreceive-broadcastreceiver – Angel Koh Aug 26 '15 at 02:07
  • Sorry Angel Koh, I had allready tried that and it is not the solution because it starts the activity.. I want to call a procedure within the activity, not start the whole procedure. Please help I am new to this and just to cant figure it out for my self. – Miguel Fernandes Aug 26 '15 at 07:38
0

try this. Dude, make the ligaInternet method static, only the static method can be referenced from class name. The statement MainActivity.ligaInternet() is now incorrect because ligaInternet() is a non static method hence it cannot be referenced from class name. and also remove the protected keyword from the method.

Arpit Agrawal
  • 321
  • 1
  • 2
  • 14
  • i think the capital "M" in "MainActivity.ligaInternet()" is a typo on the poster part. that will cause a compile error, not a crash. – Angel Koh Aug 21 '15 at 03:13
  • Just do as I said, change ligaInternet() to "public static void ligaInternet() " and let me know if you encounter any problem with this. – Arpit Agrawal Aug 21 '15 at 14:11
  • Arpit Agrawal Your sugestion does not work for me as I can not make the function ligaInternet public static. Any other sugestions? Thank you in advance guys! – Miguel Fernandes Aug 25 '15 at 20:26
0

Possible method. Put the following inside your broadcast receiver.

Intent intent2open = new Intent(context, MainActivity.class)

And now inside MainActivity create a new method as follows:

public void onNewIntent (Intent intent) { 
 ligaInternet();
  // This simply calls the function.  
  //so make sure the function is 
  //written somewhere inside  
 //MainActivity as well.
}
KISHORE_ZE
  • 1,466
  • 3
  • 16
  • 26
  • tried your sugestion and did not get a app crash like before but the ligInternet() function is not called. On the boradcast reciever I get a warning saying intent2open is never used. I think I am on the right track but not quite there yet. Any further help will be apreciated – Miguel Fernandes Aug 25 '15 at 20:23