Custom intent to other app - android

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

Related

Is it possible to receive explicit intent from different app?

I am working with a bluetooth button and Zebra TC20. I want to start zebra's scan when extra bluetooth button get clicked.
Bluetooth button is supported by their app - flic. There is an option to send Intent. So I would like to send intent to my app. This could be done by implicit intent. But I am building this app so I know exact activity when the scan should be triggered.
From what I read I should use explicit intent if I want the activity which I know the name, but everywhere explicit intent is tied within one app.
Is it possible to call specific activity of my app from another app?
This question is edited.
Look at the manual at page 52-61 everything is explained, i had to implement it with a ET55, but it seems to be the same process.
I personnaly did it using the Intent output option ith intent delivered via broadcast.
First you can make open the DataWedge App (the app should be preinstalled, it is where you configure things about the scanner)
You create a profile for your app
You click on the profile and you check the Profile enabled option
You enable barcode input and Intent ouput, disable Keystroke and ip output
You associate your app (Associated apps option)
(go to Page 75-76 of manual) You set the intent action with something like datawedge.yourapp.SCANNER_RESULT
You left category blank
You set intent delivery to Broadcast Intent
For the rest the default option should be ok
Then, in your app you have to register the broadcast receiver (in onCreate()):
//first you implement the action to be executed when it receives the broadcast
receiverZebra = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String scanResult = intent.getStringExtra("com.symbol.datawedge.data_string");
/*
do things with the barcode here
*/
}
};
//then make a filter for the broadcast
filterZebra = new IntentFilter();
filterZebra.addCategory(Intent.CATEGORY_DEFAULT);
//the action you set in step 5 in datawedge
filterZebra.addAction("datawedge.yourapp.SCANNER_RESULT");
Then in the onStart and onStop methods you can register/unregister your broadcast receiver
#Override
protected void onStart(){
super.onStart();
registerReceiver(receiverZebra, filterZebra);
}
#Override
protected void onStop()
{
super.onStop();
unregisterReceiver(receiverZebra);
}
There are other ways to implement it, there wouldn't be other option than Intent output if there were not but it works great for me. I don't think there is much differences between the TC20 and ET55 so it should work for you as well
After the question was edited
Taken from this tutorial
In the configuration app of your button, you should be able to link an intent action name to the bluetooth button. To receive it, you have to set an intent filter in your app manifest :
<activity
android:name="com.example.myapplication.activitytolaunch"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.myapplication.ACTIVITY_TO_LAUNCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
And your button will have to launch the intent : "com.example.myapplication.ACTIVITY_TO_LAUNCH"
If you want to launch the activity directly, you can use the second part of the answer. If you have to process the barcodes when your app is opened (i.e. adding the barcodes to a list), if you can tell the button to broadcast an intent, you can use the first part.

Display Alertbox from service

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

Start Activity in a BroadcastReceiver

I've built a small app. The only thing it does is, catch an outgoing call and show some activity when it happens. There is just an Activity and a BroadcastReceiver.
I wanted to integrate my code with another application, I removed the BroadcastReceiver from the Manifest.xml and created (and registered) it dynamically from the main activity. My receiver fired well but the activity is not shows up.
What is the difference between the two methods?
How can I make the activity to show up?
from MainActivity.java:
callInterceptor = new InterceptOutgoingCall();
IntentFilter callInterceptorIntentFilter = new IntentFilter("android.intent.action.NEW_OUTGOING_CALL");
callInterceptorIntentFilter.setPriority(100);
registerReceiver(callInterceptor, callInterceptorIntentFilter);
and from the function receiver.onReceive(Context,Intent):
Intent alertIntent = new Intent(context, AlertActivity.class);
alertIntent.putExtra("callnumber", phonenbr);
alertIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alertIntent);
my activity is declared in the manifest like this:
<activity android:name=".AlertActivity"
android:screenOrientation="portrait"/>
I found the answer at two threads:
Android launch an activity from a broadcast receiver
Activity started from notification opened on top of the activity stack
In the manifest, the activity should be declared with android:taskAffinity.
And when starting the intent I had to add a flag = Intent.FLAG_ACTIVITY_NEW_TASK

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 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.

Categories

Resources