Is there any way to use alertbox in service rather then making another activity for it and then starting that activity from service?
2 Answers
Create a custom Broadcast Receiver
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// AlertDialogue will be here
}
}
Register it in the manifest file
<receiver android:name="MyBroadcastReceiver">
<intent-filter>
<action android:name="com.sample.A_CUSTOM_INTENT">
</action>
</intent-filter>
</receiver>
Trigger the BroadcastReceiver from service
Intent intent = new Intent("MyCustomIntent");
EditText et = (EditText)findViewById(R.id.extraIntent);
// add data to the Intent
intent.putExtra("message", (CharSequence)et.getText().toString());
intent.setAction("com.sample.A_CUSTOM_INTENT");
sendBroadcast(intent);

- 7,397
- 5
- 44
- 59
-
broadcast does not allow alertdialog i guess. – Zyshan Noor Dec 04 '14 at 10:27
-
onReceive() is running on main (UI) thread.Then whats the problem ?Have you tried it? . – Don Chakkappan Dec 04 '14 at 10:32
-
yp I have tried it .It just crashes the application.And my logcat does not show logs from when app is running on cell phone – Zyshan Noor Dec 04 '14 at 10:35
-
Try to trace the exception – Don Chakkappan Dec 04 '14 at 10:36
A Service cannot show a Dialog
, only you can show short lived Toast
from there.
But If your Requirement is of showing dialog I will not disappoint you .You have got following options:
- If you have a
MainActivity
use aLocalBroadcastReceiver
to send a broadcast to your activity and let your MainActivity show a Dialog. (This will be possible only when your activity is visible) - The other option is Define a Dialog theme activity (take a look here) in your manifest and start that activity from service.
- Another option could be , Define a Activitiy in Manifest whose sole purpose would be display a Dialog and start that activity from service
Edit
I have a suggestion , If you wish to present information to your users , you should use Notification . The main reason that you cannot show a dialog from service is that Google wants information to be decently presented to its users , instead of surprising them when suppose they are in middle of a call , or typing message etc.
if the user is interested, then he will touch the notification and you start your own activity, possibly resuming your activity, creating a new one, and then using a dialog requesting action to be performed, or whatever you want to do.
Alternative Solution
However If you persist I have another solution if you like hacking, (but I don't like hacks in good application).

- 1
- 1

- 1,525
- 11
- 17
-
Is there any other way to show alert box from service instead of making activities. – Zyshan Noor Dec 04 '14 at 10:33
-
I have added a suggestion note and possible answer to your question :) enjoy – Shubhang Malviya Dec 04 '14 at 10:58
-
well my service checks if user is launching a certain application then an alert dialog should appear asking for password. For this i need to declare alert box in service or from broadcast rather then making another activity. – Zyshan Noor Dec 04 '14 at 11:48
-
well in that case you should definitely go with my above alternative solution , Which makes use of window manager , try to google about window manager example , and you will be satisfied :) .After implementing If you found it useful don't forget to accept answer ;) – Shubhang Malviya Dec 04 '14 at 13:01
-
its working to some extent.now m trying to make alertbox activity from it. – Zyshan Noor Dec 04 '14 at 18:53