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
Related
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.
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
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.
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();
}
}
I have a few intents that activity sends to service.
All of those are registered in manifest:
<service android:name=".location.LocationService" android:label="#string/location_service_started">
<intent-filter>
<action android:name="#string/location_service_set" />
<action android:name="#string/location_service_start" />
<action android:name="#string/location_service_stop" />
</intent-filter>
</service>
But only location_service_start and location_service_stop intents are received. What could be the reason?
There is my receiver code:
private BroadcastReceiver LocationServiceReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(getString(R.string.location_service_stop)))
{
showMessage("stop");
}
if(intent.getAction().equals(getString(R.string.location_service_start)))
{
showMessage("start");
}
if(intent.getAction().equals(getString(R.string.location_service_set)))
{
showAlertBox("set");
}
}
};
So I never see "set" message. I've even tried put sendBroadcast for "start" and "set" messages in the same place, but everything still the same. "start" - OK, "set" - never received.
Functions that fires intents:
protected void start()
{
Intent intent = new Intent(getString(R.string.location_service_start));
getApplicationContext().sendBroadcast(intent);
}
protected void set(double lat, double lon, double rad)
{
Intent intent = new Intent(getString(R.string.location_service_set));
intent.putExtra("lat", lat);
intent.putExtra("lon", lon);
intent.putExtra("rad", rad);
getApplicationContext().sendBroadcast(intent);
}
Both are correct send, without errors, actions are correct.
UPD:
Oh, my fault. I forgot to add filter.addAction... for new intent.
I'm sorry. But answers was really useful! Thank you!
All of those are registered in manifest:
Generally, you do not use string resources for action strings in an <intent-filter>, because you never want to internationalize them.
Generally, you do not use an <intent-filter> at all with a service unless you are exposing that service to third-party apps. In fact, right now, you are exposing your service to third-party apps, so anyone can send these commands to your service.
But only location_service_start and location_service_stop intents are received
No, none of them are received by the service. You are sending broadcasts in the Java code. Services do not receive broadcasts.
Functions that fires intents:
Do not use getApplicationContext() unless you know what you are doing. Whatever you are calling getApplicationContext() on is a Context, so you can just call sendBroadcast() on it.
Copy & Paste from this question I just answered. Should be the same issue.
You have to put each <action /> tag inside a seperate <intent-filter /> tag in your manifest.
This should be a bug, since the doc states you can put more than one action inside a filter tag:
Zero or more action [..] tags should be included
inside to describe the contents of the filter.
Source