How to start service in android without activity

I am new to android. I have two classes, and the first class

public class SmsReceiver extends BroadcastReceiver{} 

And second grade

 public class SMS extends Activity{} 

All I want to do: when I receive an SMS, I start work and do something. But I want to use "service" instead of "activity". I mean when the application starts, and then start the service without activity.

Is it possible?

+6
source share
3 answers

Start the 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 register your service in AndroidManifest.xml as:

 <service android:name="com.xxx.xx.Your_Service" /> 
+3
source

Yes, you can do this simply by creating a BroadCastReceiver that calls your Service when you download your application. Here is the complete answer given by me. Android - start service at boot

If you do not need any icon / launcher for the Application, you can also do this, just do not create any actions with

  <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> 

Just declare your service as normally advertised.

+1
source

When you received sms, you can start your service using the 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 specify permission on your AndroidManifest.xml file

  <uses-permission android:name="android.permission.RECEIVE_SMS"> 

and to send and receive sms you can check this tutorial Sending sms in android

0
source

All Articles