Reputation: 5248
i am beginner at android. I have two class, and first class is
public class SmsReceiver extends BroadcastReceiver{}
And the second class is
public class SMS extends Activity{}
All I want to do that : when I get an SMS, start activity and do something. But i want to use "service" instead of "activity". I mean when application start, then start service without activity.
is this possible ?
Upvotes: 6
Views: 10312
Reputation: 132972
Start your Service from SmsReceiver as:
public class SmsReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("android.provider.Telephony.SMS_RECEIVED")){
//action for sms received
// start service here
Intent intent=new Intent(context,Your_Service.class);
context.startService(intent);
}
else {
}
}
}
and make sure you have registered your service in AndroidManifest.xml
as :
<service android:name="com.xxx.xx.Your_Service" />
Upvotes: 3
Reputation: 1615
When you got an sms you can start your service by broadcast receiver
@Override
public void onReceive(Context context, Intent intent) {
try {
context.startService(new Intent(context,
YourService.class));
} catch (Exception e) {
Log.d("Unable to start ", "Service on ");
}
and pls make sure you have mention the permission to your AndroidManifest.xml file
<uses-permission android:name="android.permission.RECEIVE_SMS">
and for sms sending and receiving you can check out this tutorial Sending sms in android
Upvotes: 0
Reputation: 13785
Yes you can do that by just creating a BroadCastReceiver that calls your Service when your Application Boots. Here is a complete answer given by me. Android - Start service on boot
If you don't want any icon/launcher for you Application you can do that also, just don't create any Activity with
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Just declare your Service as declared normally.
Upvotes: 1