<service
android:name="MyService"
android:icon="@drawable/icon"
android:label="@string/service_name">
</service>
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//TODO do something useful
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
//TODO for communication return IBinder implementation
return null;
}
}
Option | Description |
---|---|
Service.START_STICKY | Service is restarted, if it gets terminated. Intent data passed to the onStartCommand method is null. Used for services which manages their own state and do not depend on the Intent data.Service is not restarted. Used for services which are periodically triggered anyway. The service is only restarted, if the runtime has pending startService() calls since the service termination. |
Service.START_NOT_STICKY | Service is not restarted. Used for services which are periodically triggered anyway. The service is only restarted, if the runtime has pending startService() calls since the service termination. |
Service.START_REDELIVER_INTENT | Similar to Service.START_STICKY but the original Intent is re-delivered to the onStartCommand method. |
// Create an explicit intent
Intent i= new Intent(context, MyService.class);
// if needed, add data to the intent
i.putExtra("Param1", "Data for the service");
context.startService(i);
public class ExampleService extends IntentService {
public ExampleService() {
super("ExampleService");
}
@Override
protected void onHandleIntent(Intent intent) {
int waitTime = (int) intent.getExtras().get("wait_time");
// do work here, e.g. download file
try {
Thread.sleep(waitTime*1000);
intent.setAction("MYSERVICE_FINISHED_ACTION");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
}
catch (InterruptedException e) {e.printStackTrace();}
}
Event | Description |
---|---|
Intent.ACTION_BOOT_COMPLETED | Boot completed. Requires the android.permission.RECEIVE_BOOT_COMPLETED permission |
Intent.ACTION_POWER_CONNECTED | Power got connected to the device |
Intent.ACTION_POWER_DISCONNECTED | Power got disconnected from the device |
Intent.ACTION_BATTERY_LOW | Battery gets low, typically used to reduce activities in your app which consume power |
Intent.ACTION_BATTERY_OKAY | Battery status good again |
public class MyPhoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Incoming phone call", Toast.LENGTH_LONG).show();
}
}
<receiver android:name="MyPhoneReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
public void onResume() {
super.onResume();
// Register mMessageReceiver to receive messages.
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("my-event"));
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
protected void onPause() {
// Unregister since the activity is not visible
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onPause();
}
Intent intent = new Intent();
intent.setAction("my-event");
sendBroadcast(intent);
Intent intent = new Intent();
intent.setAction("my-event");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);