Display Alertbox from service - android

Is there any way to use alertbox in service rather then making another activity for it and then starting that activity from service?

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 a LocalBroadcastReceiver 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).

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);

Related

Custom intent to other app

I have a simple question but i can't find anything on google, maybe i use the wrong key word.
I'm developping an app with a service in background. This service is always started. I have a phone with a custom button than can start an app. But i want to use this button to start an action on my service don't start any activity.
To do that, i have think about an ugly solution : I configure my custom button to start an other app. This app is a blanck activity and on the onCreate() event i just send an itent to my service and after finish the activity.
My question is how can i send a custom intent to an other app ?
my idea : in the blanck activity write this
#Override
public void onCreate(){
super.onCreate();
Intent customIntent = new Intent("com.customIntent.action");
startActivity(customIntent);
finish();
}
On my service doing something like that :
IntentFilter it = new IntentFilter();
it.addAction("com.customIntent.action");
registerReceiver(myReceiver, it);
Thanks for your help !
You are registering receiver and it will only be catched when sendBroadCast will be called with the intent. Secondly, you are starting Activity with that Intent action. There is no Activity in xml/code which handles this action. Thirdly, you can add this Intent Filter in AndroidManifest against specific reciever and in Activity use
Intent customIntent = new Intent("com.customIntent.action");
LocalBroadcastManager.getInstance(this).sendBroadcast(customIntent);
AndroidManifest.xml
<receiver android:name="." >
<intent-filter>
<action android:name="com.customIntent.action" />
</intent-filter>
</receiver>
Hope this helps.
I dont't know if I understand your question correctly but this tutorial explains very well how you use an intent to interact with another app:
http://developer.android.com/training/basics/intents/index.html

What is the cleanest way to create a GUIless application?

Good Morning.
I have a question about how to create an application without GUI. It should start when the user pushes the icon. Reading other posts, seems that the natural way of doing this would be a Service.
Since the app has no GUI, it makes no sense to add any Activity. For this reason, the Service has to be unbinded. So, if there is no component calling startService, and no external component is sending an intent, ¿how does the service start?
Is there any attribute in the manifest to achieve this? Or maybe extending Application and using onCreate to start the service?
Thanks.
UPDATES:
-There's no way to start a Service in the same app without an Intent. Other options would be autostart or Broadcast receivers, but these don't fit my requirements.
-Tried a test app without Activities, and the icon isn't even showing in the launcher. Don't know the reason of this, maybe related to the manifest not having a LAUNCHER activity.
The list of applications shown in the Android launcher is basically the list of all activities in the system that have a LAUNCHER intent filter:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
If you put this intent filter on a <service>, it will not work (just tried). Thus, the only way to do what you want to do is through an Activity. I think the cleanest way is something like this:
public void onCreate(Bundle savedInstanceState) {
Intent service = new Intent(this, MyService.class);
startService(service);
Toast.makeText(this, "Service started.", Toast.LENGTH_SHORT).show();
finish();
}
The user will not see anything except a small message at the bottom of the screen saying "Service started." that will automatically disappear in a couple of seconds. It's clean and user-friendly.
The service is started either when somebody calls startService() or when somebody calls bindService(). Note that if service is only started via bindService() it will be automatically stopped when Activity either explicitly unbinds from it or is destroyed (and it was the only binder).
You can declare BOOT_COMPLETED_ACTION broadcast receiver in your AndroidManifest.xml and start your service on system boot. But you service will only start on next device reboot. And there are some issues with applications without activities and this broadcast event in Android 3.1. More info can be found here.
In general, its good to have at least one activity in your application, even if your primary component is service. This activity will start the service when user launches it, and also may expose some ability to configure the service behavior.
Example of activity that starts service:
public class ServiceStarterActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
startService(new Intent(this, ServiceA.class));
finish();
}
}

how to to pop up and ask for user input on android after receiving broadcast

hey, im new to android development and trying to make my first application.
What im trying to implement is a feature i've seen in Handcent SMS: the popup notification.
So far, my application has a broadcast receiver that uses Toast to display an incoming SMS message.
However, instead of a Toast notification, I want to make a pop up window that shows the message and offers users a space to type a reply and a button to send. (also a button to simply acknowledge the message without replying)
how would I accomplish this?
can I make my own 'floating' activity and use startActivityForResult?
would that have to be fired from inside of a service since broadcast receivers arent supposed to do any heavy lifting?
or can i use NotificationManager or something.
You need to have an activity (layout+events etc) and in order to be 'floating' you need to set it's theme to dialog, this can be done in the manifest file where you define your activity
Something like
<activity android:name=".utils.TextEntryActivity"
android:label="Type in the value" android:theme="#android:style/Theme.Dialog" />
For starting other activity from BroadcastReceiver you can use the passed Context of the onReceive event.
context.startActivityForResult(...)

How can I write a Broadcast Receiver that will be invoked when user clicks on any application icon?

How can I write a Broadcast Receiver that will be invoked when user clicks on any application icon?
I tried by writing:
<receiver android:name = "myreceiver">
<intent-filter>
<action android:name = "android.intent.action.MAIN">
</intent-filter>
</receiver>
But it is not called.
I tried,
by using Packagemanager I will get ApplicationInfo. From that I can know all the application starting activity name and package names. I thought I can use them to registerReceiver
and my receiver will listen by its launching activity and package name.
But I strucked. I am unable to do that. I think I lost the way.
What can I do to solve this problem?
I don't think this is possible, there is just too much room for abuse. What are you trying to do that you would need to be notified anytime someone launched an application?
Use this technique:
//implement onClickListener on your class
Class blabla extends Activity implements onClickListener{
...
...
}
//this will force you to override the onClick method on your activity or //fragment
#override
void onClick(View V){
sendBroadcast(new Intent("clickSomewhere!"));
//Put here more actions you want to do when anything is touched/clicked
}
Basically this is it. Anytime you press a button the Broadcast will be sent. Your Broadcast receiver, could be coded in some other place, listening to incoming broadcasts. If you need more code of the broadcast sending side, or the broadcast receiving side let me know.

Android - How to display a dialog over a native screen?

I was wondering if anyone can tell if how to pop a dialog screen up over a native Android screen?
I currently have an application that traps an outgoing call and stops it, I then want to pop up a dialog that would take over from the dialler screen and alert the user that there attempt to call has been blocked and allow them have some new options from the dialog.
I know that some people will say that I should use notifications instead but I'm aware of that and its not the way that it should work, I need to be able to pop up a dialog when the call gets trapped.
This is my dialog code so far
AlertDialog LDialog = new AlertDialog.Builder(context)
.setTitle("Call Blocked")
.setMessage("Call Blocked, reroute call?")
.setPositiveButton("ok", null).create();
LDialog.show();
I presume I have to somehow get the context to be that of the dialler screen?
Can anyone offer any help and assistance or links to tutorials?
Thanks in advance
For my application I used an activity with the Dialog theme.
You can declare the theme in the manifest file :
<activity android:name="PopupActivity"
android:launchMode="singleInstance" android:excludeFromRecents="true"
android:taskAffinity="" android:theme="#android:style/Theme.Dialog" />
use launcheMode="singleInstance" and taskAffinity="" if your popup is detached from your main application. Otherwise user may click the back button and return to the previous activity of your application.
excludeFromRecents="true" to avoid your popup to appear in recent tasks (long press home)
theme="#android:style/Theme.Dialog" to set the Dialog theme.
How to get the equivalent of launchMode = singleTask in code
I have not seen a clear explanation of how to set these flags programmatically, so I will include my results here. tldr: you have to set FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_MULTIPLE_TASK.
If you launch this directly from your app, your dialog will appear on top of your app's last Activity. But if you use a PendingIntent broadcast by AlarmManager to launch your "dialog", you have time to switch to a different app so you can see that your "dialog" will appear over that other app, if the style is set appropriately to show what is behind it.
Obviously one should be responsible about when it is appropriate to display a dialog on top of other apps.
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// you have to set these flags here where you receive the broadcast
// NOT in the code where you created your pendingIntent
Intent scheduledIntent = new Intent(context, AlertAlarmActivity.class);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(scheduledIntent);

Categories

Resources