Ok, lets put some code now ..
Start with the main UI activity, which will just show a toggle button. On clicking this button, the state of responsiveness of the app changes. We'll implement onClickListener on this button through the xml file.
The corresponding code in main.xml for the button should look like
<ToggleButton
android:id="@+id/appOnOff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonStateChanged"/>
The android:id attribute specifies an unique id for this button, from which it is referenced in the java source files.
The android:onClick="buttonStateChanged" attribute handles the click events on the ToggleButton. A function declared as public with its return type as void and name buttonStateChanged will be called whenever the click event happens. So we store the state of the responsiveness of the app in a boolean variable in this function.
The corresponding code in MainActivity.java should look like
public void buttonStateChanged(View v) {
ToggleButton appOnOff = (ToggleButton)findViewById(R.id.appOnOff);
if(appOnOff.isChecked()){
state = true;
Toast.makeText(this, "Automatic response on", Toast.LENGTH_SHORT).show();
}
else{
state = false;
Toast.makeText(this, "Automatic response off", Toast.LENGTH_SHORT).show();
}
}
The variable appOnOff carries the ToggleButton object which was declared in the layout file main.xml through its referenced id. Based on the state of button (isChecked returning true or false) the boolean variable state is initialized. This value will be used by the broadcast receiver to decide whether to take action or not.
Now lets add the Broadcast Receiver for the sms broadcasts. To receive a sms we have to add this permission in our androidManifest.xml file
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
Start with the main UI activity, which will just show a toggle button. On clicking this button, the state of responsiveness of the app changes. We'll implement onClickListener on this button through the xml file.
The corresponding code in main.xml for the button should look like
<ToggleButton
android:id="@+id/appOnOff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonStateChanged"/>
The android:id attribute specifies an unique id for this button, from which it is referenced in the java source files.
The android:onClick="buttonStateChanged" attribute handles the click events on the ToggleButton. A function declared as public with its return type as void and name buttonStateChanged will be called whenever the click event happens. So we store the state of the responsiveness of the app in a boolean variable in this function.
The corresponding code in MainActivity.java should look like
public void buttonStateChanged(View v) {
ToggleButton appOnOff = (ToggleButton)findViewById(R.id.appOnOff);
if(appOnOff.isChecked()){
state = true;
Toast.makeText(this, "Automatic response on", Toast.LENGTH_SHORT).show();
}
else{
state = false;
Toast.makeText(this, "Automatic response off", Toast.LENGTH_SHORT).show();
}
}
The variable appOnOff carries the ToggleButton object which was declared in the layout file main.xml through its referenced id. Based on the state of button (isChecked returning true or false) the boolean variable state is initialized. This value will be used by the broadcast receiver to decide whether to take action or not.
Now lets add the Broadcast Receiver for the sms broadcasts. To receive a sms we have to add this permission in our androidManifest.xml file
<uses-permission android:name="android.permission.RECEIVE_SMS"/>