Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to start service in android without an activity

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 ?

like image 670
Mahmut EFE Avatar asked Dec 03 '12 10:12

Mahmut EFE


1 Answers

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" />
like image 195
ρяσѕρєя K Avatar answered Oct 27 '22 21:10

ρяσѕρєя K