0

I'm using BOOT_UP_COMPLETE BroadcastReceiver in my program. I need to start a method from my MainActivity (I found only to start an Activity or Service from a BroadcastReceiver).

In my code below, I need to start InitMorning method.

How to code it?

    public class MainActivity extends Activity {
        //private Context mContext;
        public static int isMasterThreadRunning = 0;    
        Intent intent = null;
        private boolean isActivityExitInProgress = false;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            Log.e("MainActivity : onCreate : Enter");
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            if(isMasterThreadRunning == 1)
            {
                SetButtonTitle("Stop Monitoring");
            }
            else
            {
              isMasterThreadRunning = 0;
            }
            Log.e("MainActivity : onCreate : Exit");  

        }



        /** Called when the user clicks the Send button */
        public void startMonitoring(View view) {
            if(isActivityExitInProgress == false)
            {
              if(isMasterThreadRunning == 0) {
                InitMonitoring();
              }
              else if(isMasterThreadRunning == 1) {
                stopMonitoring();
                isActivityExitInProgress = false;
              }
            }
          } 

        public void SetButtonTitle(String buttonTitle)
        {
          Button start_monitoring_button = (Button)findViewById(R.id.button_start_monitoring);
          start_monitoring_button.setText(buttonTitle);
          if(buttonTitle == "Stop Monitoring")
          {
            start_monitoring_button.setEnabled(false);
          }
        }

        public void InitMonitoring()
        {
            Log.e("MainActivity : startMonitoring : Going to start master thread : Enter");
            MasterThreadService.mContext = getBaseContext();
            SetButtonTitle("Stop Monitoring");
            intent = new Intent(this, MasterThreadService.class);
            startService(intent);

            isMasterThreadRunning = 1;
            this.moveTaskToBack(true);
            Log.e("MainActivity : startMonitoring : Going to start master thread : Exit");
        }

        public void stopMonitoring()
        {
          Log.e("MainActivity : stopMonitoring : Enter");
          isActivityExitInProgress = true;
          SetButtonTitle("Start Monitoring");
          Log.e("MainActivity : startMonitoring : Calling exitFromApp");
          // Make a call to exit and clean everything : exitFromApp
          MasterThreadService.exitFromApp();
          if(intent == null)
          {
              Log.e("MainActivity : stopMonitoring : NULL POINTER ACCESS NEED TO INVESTIGATE");
          }
          else
          {
              stopService(intent);
          }
          isMasterThreadRunning = 0;
          Log.e("MainActivity : stopMonitoring : Exit");
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

    }
Blo
  • 11,903
  • 5
  • 45
  • 99
saravana
  • 39
  • 1
  • 9
  • @Fllo thx for removing that caps lock issue. – rekire Mar 23 '14 at 13:03
  • @rekire It seemed yelling on us ^^ – Blo Mar 23 '14 at 13:05
  • @ Fllo thanks for your help.but i didnt receive my solution yet :( – saravana Mar 23 '14 at 13:10
  • @saravana Hi, did you try my answer below? Did you succeed? – Blo Mar 25 '14 at 09:09
  • @Fllo..thank you very much..yes its works :)i dont have reputation dude.. – saravana Mar 25 '14 at 14:01
  • @saravana good news! Happy coding. – Blo Mar 25 '14 at 14:02
  • @Fllo i need to launch a application in android without any user interface..is it possible?when launcher icon pressed the the app should be started..do you have any suggestions? or if you have any source code example provide me. – saravana Mar 25 '14 at 14:09
  • I don't understand clearly your question. But it seems you try to do this: http://stackoverflow.com/q/2704084/2668136 or this: http://stackoverflow.com/questions/526074/android-activity-with-no-gui.. If you cant achieve it, post another question ^^ – Blo Mar 25 '14 at 14:38

2 Answers2

0

Try making a static reference. Change

public void InitMonitoring()

To

public static void InitMonitoring()

Then call the method from your service like:

MainActivity.InitMonitoring();

This is how I would do it. Don't know if it's the best way though.

Aashir
  • 2,621
  • 4
  • 21
  • 37
  • if i change it to static then the errors say to modify all to static.it says not be able to reference non-static variables. – saravana Mar 23 '14 at 13:40
0

As I can read on it, you need to attach your Activity in your BroadcastReceiver.

You must to create your BroadcastReceiver dynamically. Then, use setMainActivityHandler(MainActivity) method in your Activity when you define and call the BroadcastReceiver and attach your Activity inside the receiver class with setMainActivityHandler(MainActivity mainactivity) method. Finally, call your Activity method with the following: mainactivity.methodInActivity();. This should do the trick.

You can find how do this on Call an activity method from a BroadcastReceiver class and you have another example on Call activity method from broadcast receiver.

Hope this helps.

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99