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;
}
}