2

I want to implement this scenario for my application. I want to schedule my service to start when the phone boots, and whenever another application calls my service I want my service to start a certain activity within the project.

So in order to be clear. I want to create a project which contains a service which runs whenever the phone boots, and is dormant, listening for a call from a third party application. And whenever that call is received this service calls an Activity (from the same project, not third party)

How can I configure my manifest file in order to achieve this?

I have also come across this suggestion but my scenario is pretty different.

Thank you very much in advance

Community
  • 1
  • 1
Libathos
  • 3,340
  • 5
  • 36
  • 61
  • *"... listening for a call from a third party application."* what call do you mean? `Context#startService()` ? – pskink Dec 05 '16 at 10:47
  • Yes, This is a 2 application suite. A main one (the third party app). And this service and activity I am asking about which provides extra features but is only called from the third party app. I hope I am clear – Libathos Dec 05 '16 at 10:50
  • so you dont need to "listen for a call", the OS does it, by "starting" your `Service` and calling its `onStartCommand` method – pskink Dec 05 '16 at 10:54
  • I want this to be just a service, to not have an icon or showing anywhere. I simply want a service, or as I found out just now, a JobService. The jobService should always run silently and when It is called by another app then the service should call an Activity – Libathos Dec 05 '16 at 11:05

2 Answers2

0
  **Define Service in manifest and  Create the BroadcastReciever with boot complete permission and listen the intent.If boot completed start the service.**

public class MyService extends Service {
            Context context = this;

            @Nullable
            @Override
            public IBinder onBind(Intent intent) {
                return null;
            }

            @Override
            public int onStartCommand(Intent intent, int flags, int startId) {

                try {
                    Intent activity = new Intent(context, MyActivity.class);
                    activity.putExtra("Message", "fromService");
                    activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    activity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(activity );
                } catch (Exception e) {
                    MyLog.printException(e);
                }
                 return super.onStartCommand(intent, flags, startId);
            }



        }
Gautam Dev
  • 399
  • 2
  • 4
0

By creating a BroadcastReceiver you can perform the service startup.

public class StartupReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Intent i = new Intent(context, ShowActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }

}

and in the manifest

<receiver
    android:name=".StartupReceiver"
    android:enabled="true"
    android:exported="true"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

This allows you to run an Activity, you can then start a Foreground Service from that activity. I just set this example because I have it ready, you can adapt it to run a service as you like.

Beppi's
  • 2,089
  • 1
  • 21
  • 38