You can try to start recording in the regular old Service in the onStartCommand(...) PendingIntent and make your PendingIntent to start recording by starting this service.
To stop the service, you cannot make a PendingIntent that stops the service, so you will need to make an announcement and use BroadcastReceiver to get the intention to stop the service.
So, you will do your service:
public class RecordingService extends Service { MediaRecorder mRecorder; ... public int onStartCommand(Intent intent, int flags, int startId) {
and your broadcast receiver:
public class StopRecordingReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { context.stopService(new Intent(context, RecordingService.class)); } }
PendingIntent to start recording:
Intent i = new Intent(context, RecordingService.class); PendingIntent pi = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent to stop recording:
Intent i = new Intent(context, StopRecordingReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
source share