Is it possible to receive explicit intent from different app? - android

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.

Related

Android Intent reopening activity

I have an issue that I have not found a solution to on this site, but if this is a duplicate question, I apologize.
I am developing an application that serves as a terminal for registering when employees start/finish work, among numerous other things. The way it works is that with NFC switched-on, they scan their NFC cards and my app reads them and ultimately sends the appropriate information to the server.
However, if the app is already open (it's supposed to be open all the time, so this is an issue) and an NFC card is scanned, it reopens the app. Of course, this is done because I have set it that way in the manifest. But I can not find a way to have my app recieve the NFC scan intent if I do not add all of these lines in the manifest:
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
I have tried just writing without the but in that case it does not read the card, but instead the program chooser comes up on the phone, or if the phone does not have an appropriate app it simply says "NFC read error".
Does anyone have a solution for this? This is the last step in my project, and I have had a lot of trouble with it, and would appreciate any help. It's probably something simple that I'm just not seeing, but I'd appreciate it either way.
Android activities have different launch modes. If you set single instance it will use already opened activity and doesn't create a new activity. You can read the new intent in override method onNewIntent()
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// read intent values here
}
For various activity elements
You can use broadcastReceiver,
- first initiate the receiver to your activity
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("whateveryouwant");
notificationBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// here you can read the intent and customize the action;
int usage = intent.getIntExtra("usage",1000);
}
}
};
second register the broadcast
registerReceiver(notificationBroadcastReceiver,intentFilter);
At end unregister to the broadcast in the onDestroy method
#Override
protected void onDestroy() {
if(notificationBroadcastReceiver != null){
unregisterReceiver(notificationBroadcastReceiver);
notificationBroadcastReceiver = null;
}
super.onDestroy();
}
after doing that instead of intenting activity you can sendBroadcast()
a little guide: https://developer.android.com/reference/android/content/BroadcastReceiver.html
hope it will be helpfull

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

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

"Private" intent filter for self app use only

I have created a file explorer and register
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.OPENABLE" />
But I don't want my internal file explorer is begin shown for user to choose when another app send implicit intent with "Openable" category. How can I do so? Should I just create my own action name?
I don't have the explicit one because I want user to choose other file explorer within my app.
If it's important that only your own app is able to start one of your components, set the exported attribute to "false" for that component in the manifest.
if you asking me - I think that for your situation the best thing is to create your own intent filter (as you sugested in your question), because if you declare your activity to receive a system built in broadcast - it have a meaning. so what's the point using this built in broadcast, if you kind of "breaking his contract" from a system point of view, and want to ignore it in some cases?
if you'll decide to use this built in filter anyway although what I've suggested, the way to achieve what you want is:
register your receiver not in the manifest, but in the onCreate() method of your first launch activity, and unregister him in the onDestroy().
that way your activity won't be registered when none of your activities are foreground - that is close to state which application is close from users point of view.
in case you are not sure how it's done, that's the way:
private BroadcastReceiver mMyCustomReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerReceiver(mMyCustomReceiver, IntentFilter.create(YOUR_FILTER_ACTION_COMMAND, YOUR_FILTER_DATA_TYPE));
}
#Override
protected void onDestroy() {
unregisterReceiver(mMyCustomReceiver);
super.onDestroy();
}
I agree with Chris Stratton and Tal Kanel.But instead you can use specific "Data" in your intent filter.Documentation says:
An Intent object that contains neither a URI nor a data type passes
the test only if the filter likewise does not specify any URIs or data
types.
So if an intent has "Openable" category and has not data part that matches your intent filter specific data part,your internal file explorer will be not shown for user.
You could use an 'activity-alias' to make a 'copy' of the activity with android:exported="false" and the private intents?
If you are willing to invoke your activity via an explicit intent (ie, giving the target component name) then you can omit having an intent filter for that activity.

Overriding the hardware buttons on Android

Is it possible to override the function of a hardware button programmically on a droid? Specifically, I'd like to be able to override the camera button on my phone programmically. Is this possible?
How to Handle Camera Button Events
As soon as camera button is pressed a broadcast message is sent to all the applications listening to it. You need to make use of Broadcast receivers and abortBroadcast() function.
1) Create a class that extends BroadcastReceiver and implement onReceive method.
The code inside onReceive method will run whenever a broadcast message is received. In this case I have written a program to start an activity called myApp.
Whenever hardware camera button is clicked the default camera application is launched by the system. This may create a conflict and block your activity. E.g If you are creating your own camera application it may fail to launch because default camera application will be using all the resources. Moreover there might be other applications which are listening to the same broadcast. To prevent this call the function "abortBroadcast()", this will tell other programs that you are responding to this broadcast.
public class HDC extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Prevent other apps from launching
abortBroadcast();
// Your Program
Intent startActivity = new Intent();
startActivity.setClass(context, myApp.class);
startActivity.setAction(myApp.class.getName());
startActivity.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(startActivity);
}
}
}
2) Add below lines to your android manifest file.
<receiver android:name=".HDC" >
<intent-filter android:priority="10000">
<action android:name="android.intent.action.CAMERA_BUTTON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
The above lines are added to your manifest file to tell the system that your program is ready to receive broadcast messages.
This line is added to receive an intimation when hardware button is clicked.
<action android:name="android.intent.action.CAMERA_BUTTON" />
HDC is the class created in step 1(do not forget the ".")
<receiver android:name=".HDC" >
The "abortBroadcast()" function is called to prevent other applications from responding to the broadcast. What if your application is the last one to receive the message? To prevent this some priority has to be set to make sure that your app receives it prior to any other program. To set priority add this line. Current priority is 10000 which is very high, you can change it according to your requirements.
<intent-filter android:priority="10000">

Categories

Resources