App not receiving MMS messages - android

I am developing an SMS/MMS messaging system and I need the app to receive both SMS and MMS messages. It receives SMS messages just fine. I am testing on a real phone (Galaxy S7 Edge running 7.01).
The app does not receive MMS messages. According to this post, my manifest is correct:
<receiver android:name="com.webnation.text2email.receivers.MMSBroadcastReceiver"
android:enabled="true"
android:permission="android.permission.BROADCAST_WAP_PUSH"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
However, setting a break point at the first line of the broadcast receiver, the broadcast receiver is never called. I am including all the right permissions.
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_MMS"/>
<uses-permission android:name="android.permission.RECEIVE_WAP_PUSH"/>
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="com.android.vending.BILLING" />
My broadcast receiver:
public class MMSBroadcastReceiver extends BroadcastReceiver {
private static final String ACTION_MMS_RECEIVED = "android.provider.Telephony.WAP_PUSH_DELIVER";
private static final String MMS_DATA_TYPE = "application/vnd.wap.mms-message";
// Retrieve MMS
public void onReceive(Context context, Intent intent) {
this.context = context; <------- break point set here.
String action = intent.getAction();
String type = intent.getType();
int typeMessage = -1;
byte[] pushData;
GenericPdu pdu = null;
Toast.makeText(context, "MMS received", Toast.LENGTH_LONG).show();
if (action.equals(ACTION_MMS_RECEIVED) && type.equals(MMS_DATA_TYPE)) {
Bundle bundle = intent.getExtras();
Timber.d("bundle " + bundle);
if (bundle != null) {
pushData = intent.getByteArrayExtra("data");
PduParser parser = new PduParser(pushData);
pdu = parser.parse();
PduHeaders headers = pdu.getPduHeaders();
Timber.d("buffer " + pushData);
String incomingNumber = new String(pushData);
int indx = incomingNumber.indexOf("/TYPE"); // not used, parse the number in MMUtils.getAddress instead
if (indx > 0 && (indx - 15) > 0) {
int newIndx = indx - 15;
incomingNumber = incomingNumber.substring(newIndx, indx);
char[] characters = incomingNumber.toCharArray();
int index = 0;
for (int i=0;i<characters.length;i++) {
if (Character.isDigit(characters[i]) ){
index = i;
break;
}
}
//indx = incomingNumber.indexOf("+");
if (index > 0) {
incomingNumber = incomingNumber.substring(index);
TelephonyManager tm = (TelephonyManager)context.getSystemService(context.getApplicationContext().TELEPHONY_SERVICE);
String countryCode = tm.getNetworkCountryIso().toUpperCase();
try {
String prefix = CountryToPhonePrefix.prefixFor(countryCode).replace("+", "");
int indexOfBaseNumber = incomingNumber.indexOf(prefix);
if (indexOfBaseNumber > -1) {
if (indexOfBaseNumber == 0) {
int lengthPrefix = prefix.length();
indexOfBaseNumber = indexOfBaseNumber + lengthPrefix;
}
incomingNumber = incomingNumber.substring(indexOfBaseNumber);
}
} catch (IllegalArgumentException ie) {
Timber.e(ie);
} catch (IndexOutOfBoundsException ie) {
Timber.e(ie);;
}
Timber.d("Mobile Number: " + incomingNumber);
}
}
int transactionId = bundle.getInt("transactionId", -1);
Timber.d("transactionId " + transactionId);
int pduType = bundle.getInt("pduType", -1);
Timber.d("pduType " + pduType);
byte[] buffer2 = bundle.getByteArray("header");
if (buffer2 != null) {
String header = new String(buffer2);
Timber.d("header " + header);
}
List<MMSMessage> textMessages = MMSUtils.getMessagesFrom(context, intent); //meat of onReceive()
sendMessages(textMessages);
} else {
Timber.e("Invalid PUSH data");
}
}
}
}
What am I doing wrong?

Adding the following to my main activity in the Manifest seems to have solved the problem:
<activity
android:name="com.webnation.text2email.main.SMSEmailActivity"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Related

Newly receive message is not retrieving from my own Default SMS App Inbox

I am working on a project in which I have made app to default sms app and getting new message body and notification through Broadcast Receiver. It is showing toast that new message has been received and also read new message body.
But problems are
Problem 1:
The newly received sms is not retrieving from my default sms app inbox and not showing in my listview.
Problem 2:
I am not able to get each and every message from each conversation
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e(TAG, "Intent recieved: " + intent.getAction());
if (intent.getAction() == SMS_RECEIVED) {
SmsMessage msg = null;
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[])bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
if (messages.length > -1) {
Log.e(TAG, "Message recieved: " + messages[0].getMessageBody());
MyNotificationManager.getInstance(context).displayNotification(messages[0].getOriginatingAddress(), messages[0].getMessageBody());
}
}
}
}
Manifest File
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-sdk android:name="org.apache.http.legacy" android:required="false"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:name="com.ughareja.whocaller.utils.App"
android:networkSecurityConfig="#xml/network_security_config"
android:largeHeap="true">
<activity android:name="com.ughareja.whocaller.activities.SplashActivity"
android:screenOrientation="portrait"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="#string/default_notification_channel_id" />
<receiver android:name=".smsReciever.SmsListener"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
<intent-filter>
<action android:name="android.provider.Telephony.SMS_DELIVER" />
</intent-filter>
</receiver>
<!-- BroadcastReceiver that listens for incoming MMS messages -->
<receiver android:name=".smsReciever.MmsReciever"
android:permission="android.permission.BROADCAST_WAP_PUSH">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
<!-- Activity that allows the user to send new SMS/MMS messages -->
<activity android:name=".smsReciever.ComposeSmsActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</activity>
<!-- Service that delivers messages from the phone "quick response" -->
<service android:name=".smsReciever.HeadlessSmsSendService"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</service>
</application>
Can you tell me How to save receive message ?
To receive, I suggest changing your manifest:
<receiver android:name=".smsReciever.SmsListener"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<receiver android:name=".smsReciever.MmsReciever"
android:enabled="true"
android:permission="android.permission.BROADCAST_WAP_PUSH">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
In addition, you will need to declare the following permissions:
<uses-permission android:name="android.permission.RECEIVE_MMS"/>
<uses-permission android:name="android.permission.RECEIVE_WAP_PUSH"/>
You shouldn't be doing so much work on the receiver, send it to an intentReceiver instead:
#Override
public void onReceive(Context context, Intent intent) {
Timber.i("Intent received: " + intent.getAction());
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
Intent intentServiceIntent = new Intent(context, SMSIntentService.class);
intentServiceIntent.putExtras(bundle);
context.startService(intentServiceIntent);
//send broadcast to networkAvailableReceiver
Intent intentNetworkBroadcastReceiver = new Intent();
intentNetworkBroadcastReceiver.setAction("Youraction.CHECK_NETWORK_CONNECTIVITY");
context.sendBroadcast(intentNetworkBroadcastReceiver);
}
}
Here's what I use to parse out the message:
private fun parseOutMessages(intent : Intent?) {
val msgsAny : Array<Any>
var sender: String?
val msgBody: String
var bundle = Bundle()
if (intent != null) {
bundle = intent.extras as Bundle
}
try {
//parses out the message
val pdus = bundle.get("pdus") as Array<Any>
val msgs = Array<SmsMessage?>(pdus.size, {null})
for (i in msgs.indices) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val format = bundle.getString("format")
msgs[i] = SmsMessage.createFromPdu(pdus[i] as ByteArray, format)
} else {
msgs[i] = SmsMessage.createFromPdu(pdus[i] as ByteArray)
}
// Here you have the sender(phone number)
sender = msgs[i]?.originatingAddress
//is the message long enough to send?
var messageBodyLength = 0
if (msgs[i] != null) {
val message = msgs[i] as SmsMessage
messageBodyLength = message.messageBody.length
}
}
} catch (e: Exception) {
e.printStackTrace()
Timber.e(e)
}
}

SMS Broadcast Receiver onReceive() method not being called

I am building an SMS application and there is one activity and a broadcast receiver. The problem is that the broadcast receiver onReceive() method is not getting called. The code for the receiver class and manifest file is given below.
Broadcast Receiver
public class SmsBroadcastReceiver extends BroadcastReceiver {
public AudioManager audioManager;
SharedPreferences sharedPreferences;
boolean b=false;
String smsBody;
final int REQUEST_CODE_ASK_PERMISSIONS = 123;
public static final String SMS_BUNDLE = "pdus";
SmsMessage smsMessage;
public void onReceive(Context context, Intent intent) {
Toast.makeText(context.getApplicationContext(),"Toast Long",Toast.LENGTH_LONG).show();
sharedPreferences = context.getApplicationContext().getSharedPreferences("Pritom", context.MODE_PRIVATE);
Bundle intentExtras = intent.getExtras();
String format = intentExtras.getString("format");
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
String smsMessageStr = "";
String pass3 = sharedPreferences.getString("password", null);
for (int i = 0; i < sms.length; ++i) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
smsMessage = SmsMessage.createFromPdu((byte[]) sms[i], format);
} else {
smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
}
smsBody = smsMessage.getMessageBody().toString();
String address = smsMessage.getOriginatingAddress();
smsMessageStr += "SMS From: " + address + "\n";
smsMessageStr += smsBody + "\n";
}
/*if (smsBody.equals("#general" + pass3)) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(audioManager.RINGER_MODE_NORMAL);
}*/
Toast.makeText(context, "SMS : " + smsBody + pass3, Toast.LENGTH_SHORT).show();
Log.d("Message:", smsBody + pass3);
}
}
}
The problem here is that the Toast message just below onReceive() is not being displayed which indicates that the method onReceive() is not being called.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mytrendin.inappmessaging">
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsBroadcastReceiver" android:exported="true" >
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
I have used Android 5.0.1 for testing my application.
Thank you for your help :)
Try setting the higher priority android:priority="2147483647" in receivers intent-filter:
<receiver android:name=".SmsBroadcastReceiver" android:exported="true" >
<intent-filter android:priority="2147483647" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

Android Manifest code for Parse Push Receiver

I have followed the guidelines mentioned in parse.com and tried all helps from stackoverflow. But I am unable to receive any Parse Push Notification. The Dashboard shows Pushes Sent 0. Can anyone help me out on this. The code for
manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phpand"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.phpand.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.phpand.permission.C2D_MESSAGE" />
<application
android:name=".MainApp"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light" >
<activity android:name=".ParseMainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.parse.PushService" />
<receiver
android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.phpand" />
</intent-filter>
</receiver>
<receiver
android:name=".MyCustomReceiver"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
</application>
</manifest>
where MainAppp initializes the Parse, ParseMainActivity's button click sends the push message and MyCustomReceiver is the BroadCastReceiver Class.
MyCustomReceiver class has:
public class MyCustomReceiver extends BroadcastReceiver {
private static final String TAG = "MyCustomReceiver";
public static final String intentAction = "SEND_PUSH";
#Override
public void onReceive(Context context, Intent intent) {
if (intent == null) {
Log.d(TAG, "Receiver intent null");
} else {
processPush(context, intent);
}
}
private void processPush(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "got action " + action );
if (action.equals(intentAction))
{
String channel = intent.getExtras().getString("com.parse.Channel");
try {
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
Iterator<String> itr = json.keys();
while (itr.hasNext()) {
String key = (String) itr.next();
if (key.equals("customdata")) {
launchSomeActivity(context, json.getString(key));
triggerBroadcastToActivity(context);
createNotification(context);
}
Log.d(TAG, "..." + key + " => " + json.getString(key));
}
} catch (JSONException ex) {
Log.d(TAG, "JSON failed!");
}
}
}
public static final int NOTIFICATION_ID = 45;
private void createNotification(Context context) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(
R.drawable.ic_launcher).setContentTitle("Successfully logged in");
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(45, mBuilder.build());
}
private void launchSomeActivity(Context context, String datavalue) {
Intent pupInt = new Intent(context, ShowPopUp.class);
pupInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
pupInt.putExtra("customdata", datavalue);
context.getApplicationContext().startActivity(pupInt);
}
private void triggerBroadcastToActivity(Context context) {
LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(intentAction));
}
}
Probably I must be missing something in the Manifest file, any help would save my week's due.

Cannot Receive SMS with broadcast

I just need to get incoming SMS to handle it, but nothing is called from onRecieve method. everything looks just ok , but nothing is happened when i receive SMS!
here is my manifest tags inside application tags :
<receiver android:name="com.chargeirancell.key0098.view.RubinAppWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/rubinwidgetinfo" />
</receiver>
<receiver android:name="com.chargeirancell.key0098.RubinRecieveSMS">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
here also are permissions outside application tag:
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
this my receiver class :
public class RubinRecieveSMS extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("SMS", "HI");
if (ChargeApp.mShared.getBoolean("sms", true)) {
Log.i("SMS", "HI");
String number = "";
String message = "";
Bundle bundle = intent.getExtras();
if (bundle != null) {
try {
int i;
Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] currentMessages = new SmsMessage[pdusObj.length];
for (i = 0; i < currentMessages.length; i++) {
currentMessages[i] = SmsMessage
.createFromPdu((byte[]) pdusObj[i]);
number = currentMessages[i].getOriginatingAddress();
message = message
+ currentMessages[i].getMessageBody();
}
String pin;
if (number.contains("3453")) {
pin = message.substring(message.indexOf("ز:"),
message.indexOf("ک")).replace("ز:", "");
Intent i1 = new Intent(context,
ActivityDialogCharge.class);
i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
i1.putExtra("pin", pin);
context.startActivity(i1);
} else if (number.contains("8801 9574")) {
pin = message.substring(message.indexOf("ز:"),
message.indexOf("ک")).replace("ز:", "");
Intent i1 = new Intent(context,
ActivityDialogCharge.class);
i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
i1.putExtra("pin", pin);
context.startActivity(i1);
}
} catch (Exception e) {
}
}
}
}
}
even Hi is not printed in logCat!
can anyone please help me on this?
Replace code into your manifest file:
<receiver android:name=".RubinRecieveSMS" android:exported="true" >
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

Intent Filter not working for Android Beam NFC

I am sure this is simple but I cannot figure it out. All I am trying to do is send a message via NFC (android Beam) and open my App on the receiver device. I did test my code on a new Project and it worked perfectly but if I try it on my real Project it just start "New Tag collected" and shows application/eu.freemoser.mydebts2go (see my screenshot). I don't know what's wrong maybe something with the manifest? The Google results doesn't match with my problem (or I am just to stupid) but I believe I found some related answer unfortunately I still was not able to solve my problem
My Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="eu.freemoser.myDebts2go"
android:versionCode="16"
android:versionName="1.1.0">
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-feature android:name="android.hardware.nfc" android:required="false" />
<uses-feature android:name="android.hardware.camera" />
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:name="eu.freemoser.myDebts2go.MyApplication"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Mydebts2go">
>
<service android:name="eu.freemoser.myDebts2goService.NotificationService"></service>
<service android:name="eu.freemoser.myDebts2goService.SynchronizationService"></service>
<service android:name="eu.freemoser.myDebts2goService.SynchronizService"></service>
<service android:name="eu.freemoser.myDebts2goService.AwesomeSynchronizService"></service>
<activity
android:name="eu.freemoser.myDebts2go.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="eu.freemoser.myDebts2GoActivities.AwesomeDetailActivity"
android:screenOrientation="portrait" android:theme="#style/Theme.MyDebts2GO.Detail">
</activity>
<activity android:name=".AndroidBeamActivity" android:screenOrientation="portrait"
android:theme="#android:style/Theme.DeviceDefault.Wallpaper.NoTitleBar">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/eu.freemoser.myDebts2go" />
</intent-filter>
</activity>
<activity android:name="eu.freemoser.myDebts2GoActivities.DriveRestoreActivity"></activity>
<activity android:name="eu.freemoser.myDebts2GoActivities.SynchronizActivity"></activity>
<activity android:name="eu.freemoser.myDebts2GoActivities.SettingActivity">
<intent-filter>
<action android:name="android.intent.action.MANAGE_NETWORK_USAGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="eu.freemoser.myDebts2GoActivities.DatePickerActivity"></activity>
<activity android:name="eu.freemoser.myDebts2GoActivities.LocationPickerActivity"></activity>
<activity android:name="eu.freemoser.myDebts2GoActivities.SearchActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
<activity
android:name="eu.freemoser.myDebts2GoActivities.DriveAuthorzingActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar">
</activity>
<activity
android:name="eu.freemoser.myDebts2GoActivities.FastModusShortcutActivity"
android:exported="true"
android:theme="#android:style/Theme.Holo.Light.NoActionBar">
</activity>
<!-- android:value="API_KEY" /> DEBUG-->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="API_KEY" />
<meta-data
android:name="com.google.android.gms.version"
android:value="VERSION" />
</application>
</manifest>
The Fragment (sender)
public class AwesomeDetailFragment extends Fragment implements ObservableScrollView.Callbacks, NfcAdapter.CreateNdefMessageCallback, NfcAdapter.OnNdefPushCompleteCallback {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
setUpAndroidBeam();
return mRootView;
}
private void setUpAndroidBeam() {
PackageManager pm = getActivity().getPackageManager();
// Check whether NFC is available on device
if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
// NFC is not available on the device.
}
// Check whether device is running Android 4.1 or higher
else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
// Android Beam feature is not supported.
} else {
myNfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
manageNfc();
}
}
private void manageNfc() {
if (myNfcAdapter != null) {
myNfcAdapter.setNdefPushMessageCallback(this, getActivity());
myNfcAdapter.setOnNdefPushCompleteCallback(this, getActivity());
}
}
...
#Override
public NdefMessage createNdefMessage(NfcEvent event) {
Time time = new Time();
time.setToNow();
String text = myBetrag + "//" + myTitle + "//" + myContactName + "//" + myStatus + "//" + myDebtDate + "//" + myCreateDate + "//" + myRemamberDate + "//" + myNote;
NdefMessage msg = new NdefMessage(
new NdefRecord[]{createMimeRecord(
"application/eu.freemoser.myDebts2go", text.getBytes())});
return msg;
}
public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
NdefRecord mimeRecord = new NdefRecord(
NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
return mimeRecord;
}
#Override
public void onNdefPushComplete(NfcEvent event) {
}
}
The Activity (receiver)
public class Beam extends Activity {
private Long userID = null;
private Long adressID = null;
private DBAdapter myDb;
//NFC
private String myBetrag;
private String myTitle;
private String myContactName;
private String myDebtDate;
private String myCreateDate;
private String myStatus;
private String myRemamberDate;
private String myNote;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
protected void onResume() {
super.onResume();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
processIntent(getIntent());
}
}
void processIntent(Intent intent) {
myDb = new DBAdapter(this);
myDb.open();
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
// only one message sent during the beam
NdefMessage msg = (NdefMessage) rawMsgs[0];
// record 0 contains the MIME type, record 1 is the AAR, if present
String temp = new String(msg.getRecords()[0].getPayload());
String[] arrrayTemp = temp.split("//");
try {
myBetrag = arrrayTemp[0];
myTitle = arrrayTemp[1];
myContactName = arrrayTemp[2];
myStatus = arrrayTemp[3];
myDebtDate = arrrayTemp[4];
myCreateDate = arrrayTemp[5];
// can be "NOT"
myRemamberDate = arrrayTemp[6];
myNote = arrrayTemp[7];
// checkValues
channgeStatus();
checkIfNot();
checkUser();
//write to database
write();
} catch (Exception ex) {
this.finish();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
myDb.close();
}
private void checkUser() {
//do some stuff
...
}
private void write() {
//do some stuff
...
}
private void checkIfNot() {
//do some stuff
...
}
private void channgeStatus() {
//do some stuff
...
}
#Override
public void onNewIntent(Intent intent) {
// onResume gets called after this to handle the intent
setIntent(intent);
}
}
Android's MIME type matching for intent filters is case-sensitive (eventhough the MIME types themselves are not). Therefore, with Android (and also pretty much everywhere you use them) you should stick to the convention to use MIME types with lowercase letters only.
Specifically with MIME type records received over NFC, Android will automatically convert them to all-lowercase letters to overcome the problem of case-sensitivity in intent filters. So in your example, changing the type name in the intent filter to "application/eu.freemoser.mydebts2go" should work:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/eu.freemoser.mydebts2go"/>
</intent-filter>
In addition, you should also make sure you send the MIME type in lowercase letters only:
NdefMessage msg = new NdefMessage(
new NdefRecord[]{createMimeRecord(
"application/eu.freemoser.mydebts2go", text.getBytes())});

Categories

Resources