I'm trying to realize a simple Android application to read from an NFC tag.
I followed the official documentation (https://developer.android.com/guide/topics/connectivity/nfc) to actually realize an application that is almost equal to the one created by "codexpedia" -> Source code.
When a tag is near the smartphone the onNewIntent method is call, but, when I try to get data through the method getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES), I always obtain null.
You can find the code that is causing the problem at the following direct link link
Is there anyone that knows why it happens?
NdefFormatable technology means the card is capable of storing a Ndef message once it has been formatted.
So basically there is no Ndef message on this card, once it has been formatted and a Ndef message put on it then the code will be able to read this card.
You can use Apps like NFC tools or NXP TagWriter App to Format and add an Ndef message
Once formatted it should show the NfcV, Ndef technologies.
The bug in the code https://github.com/codexpedia/android_nfc_read_write/blob/master/app/src/main/java/com/example/peng/nfcreadwrite/MainActivity.java
Line 80 - IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
Should be
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
As it assumes that all cards have a Ndef message to read from, this is not the case for unformatted cards, bank cards, A lot of Transport cards, etc
Also line 91 to 93 - if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Should be
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Related
I am writing my first Android app which includes the NotificationListenerService for the purpose of notification mirroring to other devices. I noticed for messaging applications subsequent messages from the same sender post with the same ID/key and only the latest message text appears in the "android.text" extra field.
My goal is to grab the previous unread messages which look to be attached to the Notification under "android.messages" extra. My issue is that this does not read out to a char sequence or string array, instead it looks like some class which implements Parcellable is stored here.
I'm hoping there is some standard Android class used here that I'm missing which I can use to deserialize to in order to get the previous messages.
Some code:
val pArray: Array<Parcelable>? = bundle.getParcelableArray("android.messages") // Parcelable[6]#17975
println(pArray?.get(0)?.describeContents()) // 1 == CONTENTS_FILE_DESCRIPTOR
val charSequence: Array<CharSequence>? = bundle.getCharSequenceArray("android.messages") // null
val list = bundle.getStringArrayList("android.messages") // null
val arr = bundle.getStringArray("android.messages") // null
If anyone stumbles upon this thread, I figured out how to do it:
val pArray: Array<Parcelable> = bundle.getParcelableArray("android.messages") as Array<Parcelable>
val messages = Notification.MessagingStyle.Message.getMessagesFromBundleArray(pArray)
I got this simple problem whenever I say "search music click" its searching for 'search music click' too it's supposed to search my music file name click. I tried to put different variables but only boolean is the one that not causing an error.
if(voice_result.indexOf("search music") != -1){
voice_result.contains(voice_result)
String a = voice_result;
Intent intent = new Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
intent.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, MediaStore.Audio.Media.ENTRY_CONTENT_TYPE);
intent.putExtra(SearchManager.QUERY, a);
startActivity(intent);}
the voice_result here is the process result of my voice and if ever I try to search a music from voice command it always search "search music risky" instead of "risky".
You should write a parser/filter for whatever returned from the voice recognition api first. For example, you say "search music risk":
Remove unnecessary or confusing strings like "search" "music" from the raw data. I suggest you use regex matcher and map the with a set of commands with values.
After filtering the raw data (now only has "risk"), use the result for next job
I'm attempting to send an image to Hangouts from within an app I'm building.
I'm working in Xamarin for VS 2015 to do this so the code below is c# but it's not much different from the equivalent Java code so I think it's easy to follow.
What I've done is set up a button on my app which has code setting up an Intent to share an image to Hangouts. I've set the image up already in the Downloads folder on the device and hardcoded the name into the code.
Intent hangoutsShareIntent = new Intent(Intent.ActionSend);
hangoutsShareIntent.SetType("image/jpeg");
hangoutsShareIntent.SetPackage("com.google.android.talk");
string downloadsPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
string filePath = Path.Combine(downloadsPath, "shared.jpg");
hangoutsShareIntent.PutExtra(Intent.ExtraStream, filePath);
StartActivity(Intent.CreateChooser(hangoutsShareIntent, "Share with"));
When I run this, I get the option to select a chat in Hangouts that I want to send the content to. Upon selecting the chat, I get a blank message box and no image.
I've swapped the above code over to use text/plain and pass the filePath variable to the message. When I copy the file path into Chrome to check it, the image loads so I have to figure that the image is where I've said it is... right?
I get no errors (probably because the issue is in Hangouts rather than my app so I have nothing to debug there). Logcat shows nothing except an error I can't find much about on Google: ExternalAccountType﹕ Unsupported attribute readOnly
The only information I could find on that error implied some issue with permissions but I've made sure my app has runtime permissions checked for Read/Write using this code (which wraps the above):
if ((CheckSelfPermission(Permission.ReadExternalStorage) == (int)Permission.Granted) &&
(CheckSelfPermission(Permission.WriteExternalStorage) == (int)Permission.Granted))
NOTE: I'm running this on a HTC One M8 - no SD card but does have external storage on device. I've also added the above permissions to the manifest for earlier Android versions.
The documentation for this (here) isn't overly helpful either so any advice AT ALL here is welcome :)
Thanks!
If you use the file provider instead of sending just the URI on its own. This should get around the permission issues you are seeing.
There is a guide available here which might be useful.
Intent shareIntent = new Intent(Intent.ActionSend);
shareIntent.SetType("image/gif");
Java.IO.File file = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory + "/myimage.gif");
Android.Net.Uri fileUri = Android.Support.V4.Content.FileProvider.GetUriForFile(this, "com.myfileprovider", file);
shareIntent.SetPackage("com.google.android.talk");
shareIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
shareIntent.PutExtra(Intent.ExtraStream, fileUri);
StartActivity(Intent.CreateChooser(shareIntent, "Share with"));
I am trying to follow the gcm tutorial from Googles docs. They say to call this method if Play Services are out of date:
GooglePlayServicesUtil.getErrorDialog(resultCode, activity, 9000).show();
That's fine, but it puts a dialog that says "This app won't run unless you update Google Play Services" with an "Update" button. I want to change the title and the message text. My users CAN skip the update, and the app will still run. They just won't get push notifications. How do I change the message of the dialog?
I would like to do something like:
Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(resultCode, activity, 9000);
errorDialog.setTitle("out of date");
errorDialog.setMessage("Please update or push notifications won't work. Thank you");
errorDialog.show();
You can override desired string value in your application's strings.xml like this. Simply add these lines in your strings.xml
For message on dialog
<string name="common_google_play_services_update_text" msgid="448354684997260580">This app won\'t run unless you update Google Play services.</string>
For title on dialog
<string name="common_google_play_services_update_title" msgid="6006316683626838685">out of date</string>
EDIT
You could find more information here
http://blog.elsdoerfer.name/2010/04/08/android2po-managing-android-translations/
and What's the meaning of attribute 'msgid' in strings.xml?
msgid is used in android internal strings for localization but I never found any documentation about it. Two reference I have as above. I believe if you remove the msgid still it would work, although I never tried it.
The source of this code is
android_sdk\extras\google\google_play_services\libproject\google-play-services_lib\res\values\common_strings.xml
I've a problem when I try to write a NFC A tag on Android 2.3.6 (nexus S). I use the code from this example:
http://www.jessechen.net/blog/how-to-nfc-on-the-android-platform/
More precisely, when I do the Ndef.get(mytag) I get null so I cannot write my tag.
Here is the code from which I get a null references (the 'tag' value is not null), only the ndef.
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
ndef.connect();
if (!ndef.isWritable()) {
return false;
}
if (ndef.getMaxSize() < size) {
return false;
}
ndef.writeNdefMessage(message);
return true;
}
Thank you for you help !!!
Your tag may not yet be formatted for NDEF message storage or may not be able to store NDEF messages at all.
Check whether NdefFormatable.get(tag) returns something unequal to null.
Then use NdefFormatable.format(message) to try to write your message.
If NdefFormatable.get(tag) returns null, then either Android has no means to format the tag or the tag is incompatible to NDEF storage.
(Alternatively, you may want to use TagWriter, https://market.android.com/details?id=com.nxp.nfc.tagwriter to format and write your tag.)
Try NfcA.get(tag) instead. Not sure why you are getting the error though, but trying the other class might work.