So i have a simple Question , it is Possible to Handle the Method onActivityResult() in a Service if this Activity was Started from the Same Service (Using Intent) ?
In My Case , i want to start SpeechRegnition , Speak , and Get the Result on the Service , Everything on Background (Main Service Start from a Widget) ,
Thanks .
onActivityResult is the callback you have on the first activity to grab the contacts you choose. Follow this answer to receive notifications.
onActivityResult - resultCode is always 0.
The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.
Thanks for a recent downvoting, whoever it was. The previous answer I gave back in 2012 is a total nonsesnse so I decided to write a proper one.
You can not handle Activity result in a Service, but you can pass any data retrieved from onActivityResult() to a Service.
If your Service is already running, you can call startService() with a new Intent handling the event, like so
@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CODE && resultCode == RESULT_OK) {
notifyService(data);
}
}
private void notifyService(final Intent data) {
final Intent intent = new Intent(this, MyService.class);
intent.setAction(MyService.ACTION_HANDLE_DATA);
intent.putExtra(MyService.EXTRA_DATA, data);
startService(intent);
}
And handle action in a Service. If it is already running it will not be restarted, otherwise it will be started
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
final String action = intent.getAction();
if (action != null) {
switch (action) {
case ACTION_HANDLE_DATA:
handleData(intent.getParcelableExtra(EXTRA_DATA));
// Implement your handleData method. Remember not to confuse Intents, or even better make your own Parcelable
break;
}
}
}
return START_NOT_STICKY;
}
Open Transparent activity from service and use BroadcastReceiver in service. Follow the steps in detail.
1. Open transparent activity from Service
Intent i = new Intent(mContext, FloatingServiceSupportActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("action", "SpeechRegnition");
mContext.startActivity(i);
// For transparent activity use this code in AndroidManifest.xml
<activity
android:name=".FloatingServiceSupportActivity"
android:theme="@style/Theme.Transparent" />
2. Create BroadcastReceiver in Service
BroadcastReceiver brOnActivityResult = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO:
}
};
3. Register this broadcast in onCreate of Service
IntentFilter brintent = new IntentFilter();
brintent.addAction("brActionFloatingServiceOnActivityResult");
mContext.registerReceiver(brOnActivityResult, brintent);
4. Unregister this broadcast in onDestroy of Service
mContext.unregisterReceiver(brOnActivityResult);
5. Do work in Activity by using startActivityForResult and Send broadcast from Activity's (FloatingServiceSupportActivity) onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Intent i = new Intent();
i.setAction("brActionFloatingServiceOnActivityResult");
i.putExtra("action", "initTextToSpeech");
mActivity.sendBroadcast(i);
mActivity.finish();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With