Reputation: 1291
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
Upvotes: 0
Views: 3020
Reputation: 5598
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" />
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;
Upvotes: 0
Reputation: 4433
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);
Upvotes: 1
Reputation: 27549
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
Upvotes: 1
Reputation: 83303
No this is not possible. The user has to explicitly start your application him or herself before you can start your Service
. Sorry.
Upvotes: 0