6

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 ?

Mahmut EFE
  • 5,137
  • 5
  • 46
  • 56

3 Answers3

3

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" />
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

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.

Community
  • 1
  • 1
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
0

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

Vivek Bajpai
  • 1,617
  • 1
  • 19
  • 35