0

Iam new to android.I have written a service within my app..Iam installing my app(Not launching).. at this time my service should start run and open a UI..How to do this

Sudarshan
  • 1,291
  • 3
  • 26
  • 35

4 Answers4

1

You can not start your UI or Service after installing of Application, But what you can do is you can listen some Intent Action. And start whatever you want from your Broadcast Receiver.

You need to register receiver in manifest with one action like PHONE_STAT_CHANGE, message received, SCREEN UNLOCK.. there are plenty of intents you can listen in ur app and start whatever u want.

P.S:- ACtion names are not correct search on developer site

AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • if i write my service seperately and if i know the activity class name..is it possible to start that activity class from the service – Sudarshan Jul 30 '12 at 05:54
  • Absolutely you can start Activity from Service. BUt u need to be carefull about user experience.. – AAnkit Jul 30 '12 at 05:57
  • Sorry for asking too many questions.. I had written a service within my application as a separate process..Is it possible to start the service before launching the application..and can that service can start the application ui(from where it is get started). – Sudarshan Jul 30 '12 at 08:20
0

No this is not possible. The user has to explicitly start your application him or herself before you can start your Service. Sorry.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • @Alex... Thanks for ur reply.. Is it possible to start a ui activity from a service – Sudarshan Jul 30 '12 at 04:52
  • See this [**link**](http://stackoverflow.com/a/3607934/844882). I would be careful about doing that though... make sure you respect the user experience. The user will uninstall your app right away if `Activity`s start launching on him/her unexpectedly :P – Alex Lockwood Jul 30 '12 at 04:59
  • Sorry for asking too many questions.. I had written a service within my application as a separate process..Is it possible to start the service before launching the application..and can that service can start the application ui(from where it is get started). – Sudarshan Jul 30 '12 at 08:21
0

Deepak you can start a service a by Broadcast receiver by specifying on what basis you want to activate the Broadcast receiver, you can declare Broadcast receiver in menifest like

<receiver android:name=".BroadCast"
              android:enabled="false"> 
            <intent-filter> 
                <action android:name=
                    "android.intent.action.PHONE_STATE" /> 
            </intent-filter> 
        </receiver>

It shall be activate when a phone call is received

and you can call your service like

public class BroadCast extends BroadcastReceiver{
    Context context = null;
    @Override
    public void onReceive(Context context, Intent intent) 
    {

            Intent dndService = new Intent(context,
                    ContactService.class);
            dndService.putExtra("phone_nr", number);
            context.startService(dndService);
        }
    } 

When the phone will ring you broadcast receiver shall be activated and shall launch a service and from the service you can launch an activity like

public class ServiceExample extends IntentService {



    public ServiceExample () {
        super("ServiceExample ");
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub

        Intent.intent = new Intent(this , Example.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent );

}
}  

and you need to add this line before you cann the activity otherwise it shall give you a crash

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Avi Kumar
  • 4,403
  • 8
  • 36
  • 67
0

I would think best think to is first of all to make your service 'return START_STICKY' in your service onStartCommand .

Then you can add a boot listener as follows; 1. To your manifest, request the permission:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  1. Then have a broadcast listner setup to listen for bootup:

    public class MyBroadcastreceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
      Intent startServiceIntent = new Intent(context, MyService.class);
      context.startService(startServiceIntent);
    }
    

Your service will run indefinately, and if stopped due to loss of memory, it can be restarted automatically when resources are available;

Isaac Sekamatte
  • 5,500
  • 1
  • 34
  • 40