Introduction
In this article, we will talk about the following topics:
- Passing extra data by Intent and getting this data
- Start/Stop a service
- How to use a background service and its registration to AndroidManifest.xml file
- Handler and Runnable objects
In order to cover these topics, we will create a very simple application that sends lots of SMSs in a specific time sequence.
You may install this app on Google Play.
Background
To understand this article, the reader should know Java and Android platform.
Using the Code
Before you start coding, the structure of the application should be clear in the coder’s mind. For this demo application, we may follow the simple steps shown below:
- Get the needed information from user such as destination number, time sequence, etc. on the
<span style="FONT-SIZE: 10pt; BORDER-TOP: windowtext 1pt; FONT-FAMILY: Consolas; BORDER-RIGHT: windowtext 1pt; BORDER-BOTTOM: windowtext 1pt; PADDING-BOTTOM: 0in; PADDING-TOP: 0in; PADDING-LEFT: 0in; BORDER-LEFT: windowtext 1pt; PADDING-RIGHT: 0in">MainActivity</span>
. - Pass this information to a service by Intent to handle SMS sent action.
- Write a handler and runnable to sent SMS periodically.
In this demo, there are 2 classes:
1. Getting the Needed Information From User
A) Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Number:" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/numberText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message :" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/messageText" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Troll Sequence :" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/sqence"
android:text="15"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/trollBtn"
android:text="Start Trolling" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stop"
android:text="Stop Trolling" />
</LinearLayout>
B) MainActivty.java
public class MainActivity
extends Activity {
EditText number;
EditText message;
Button send;
Button stop;
EditText squence;
Context context=this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number = (EditText)
findViewById(R.id.numberText);
message = (EditText)
findViewById(R.id.messageText);
squence = (EditText)
findViewById(R.id.sqence);
send = (Button) findViewById(R.id.trollBtn);
stop = (Button) findViewById(R.id.stop);
send.setOnClickListener(new OnClickListener()
{
@Override
public
void onClick(View v) {
}
});
stop.setOnClickListener(new OnClickListener()
{
}
});
}
}
2. Pass Information to Service by Intent
- Here, we are creating our Intent object and initializing it by context and the class that we want to activate.
- We set a flag to our intent (
Intent.FLAG_ACTIVITY_NEW_TASK
) object so that trollService.class
will not be created if it is already there. Likely, there is another flag called FLAG_ACTIVITY_MULTIPLE_TASK
and if we would use this flag, our trollService.class
will be created although there is a trollService
running. - By using the method
putExtra
, first we give the key and then we give its value so that we are able to pass a data to another Service or Activity. - We are starting the service by using the method
startService(trollIntent)
which takes an intent object. Likely, we may start some Activities by using context.startActivity(Intent)
or we may start several activities at the same time as follows: context.startActivities(Intents)
in this case, intents is an array of Intent objects. - To stop an Activity or a Service we are using:
contex.stopService(Intnet)
context.stopActivity(Intent)
Intent trollIntent = new Intent
(context,trollService.class);
trollIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
trollIntent.putExtra("Number", number.getText().toString());
trollIntent.putExtra("Message", message.getText().toString());
trollIntent.putExtra("Squence",
Integer.parseInt(squence.getText().toString()));
context.startService(trollIntent);
Finally, our MainActivity
will be like this:
number = (EditText) findViewById(R.id.numberText);
message = (EditText) findViewById(R.id.messageText);
squence = (EditText) findViewById(R.id.sqence);
send = (Button) findViewById(R.id.trollBtn);
stop = (Button) findViewById(R.id.stop);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent trollIntent =
new Intent(context,trollService.class);
trollIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
trollIntent.putExtra
("Number", number.getText().toString());
trollIntent.putExtra
("Message", message.getText().toString());
trollIntent.putExtra("Squence",
Integer.parseInt(squence.getText().toString()));
context.startService(trollIntent);
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent trollIntent =
new Intent(context,trollService.class);
trollIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.stopService(trollIntent);
}
});
3. Write a Handler and Runnable in the Service Class to Sent SMS Periodically
For more information about the service, please check my other article here.
What is Runnable and Handler?
Runnable
object can be considered as a command that can be sent to the message queue for execution, handler
object is there to help the Runnable
object to sent this command to the queue.
Our Service Class
- Here, to send a message, we create our
SmsManager
object and using the method of sentTextMessage
, we initialize the message content by taking the data from intent using intent.getExtra()
. - Then, we create a
Runnable
object to write our fun function into queue so that it can be performed. To make it in a specific period, we create a handler and set its time sequence.
You may think that Runnable
object is responsible for writing the method inside itself to queue and handler sets this writing time sequence periodically so that SMS is sent periodically.
final Handler handler=new Handler();
final Runnable r = new Runnable()
{ int i=1;
public void run()
{ if(!run)
return;
handler.postDelayed(this,
intent.getExtras().getInt("Squence")*1000);
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage
(intent.getExtras().getString("Number"),
null,intent.getExtras().getString
("Message"), null, null);
Toast.makeText(getBaseContext(),+i+
"SMS Sent", Toast.LENGTH_LONG).show();
i++;
}
};
handler.postDelayed(r, intent.getExtras().getInt("Squence")*1000);
Conclusion
- Do not trust what I said in this article because I might have them wrong, just use this article to be familiar with service, smsmanager, etc.
- Try to write by yourself instead of running my demo code here.
- As you see, flags are really important, take a close look at them.
- For more powerful applications, try to get the information from developer.android.com.
References
- http://blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/
- http://developer.android.com/reference/android/os/Handler.html