i have to write a simple app to read a ISO B card
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tag_viewer);
mTagContent = (LinearLayout) findViewById(R.id.list);
mTitle = (TextView) findViewById(R.id.title);
resolveIntent(getIntent());
}
void resolveIntent(Intent intent) {
// Parse the intent
String action = intent.getAction();
Log.e(TAG, "toto ");
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
try{
myTag.connect();
if (myTag.isConnected()){
byte[] response = myTag.transceive(command);
//logText(new String(response));
Log.e(TAG, "Result " + response);
}
myTag.close();
}catch (IOException e) {
e.printStackTrace();
}
} else {
Log.e(TAG, "Unknown intent " + intent);
finish();
return;
}
}
my problem is i don't success to enter in the test if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)). I systematicaly have "Unknow intent".
concerning the manifest :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.nfc">
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
>
<activity android:name="TagViewer"
android:theme="#android:style/Theme.NoTitleBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="#xml/nfc_tech_filter" />
</activity>
</application>
<uses-sdk android:minSdkVersion="9" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
thanks for your help.
Your Problem seems to be, that you're calling resolveIntent in the onCreate Method.
This should do the Trick:
#Override
protected void onNewIntent(Intent pIntent) {
super.onNewIntent(pIntent);
resolveIntent(pIntent);
}
To get the Tag Object, I'm just using
Tag tagFromIntent = pIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NfcA tag = NfcA.get(tagFromIntent);
in my onNewIntent-Method
Unfortunately I couldn't verify this for NfcB.
You need to create a NfcAdapter object
private NfcAdapter mNfcAdapter;
than associate the object with your NFC in the onCreate method
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
do some checks before start the application, to be sure that the phone has the NFC
if (mNfcAdapter == null) {
// Stop here, we definitely need NFC
Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
finish();
return;
}
on the onResume method enable the Dispatch
#Override
public void onResume() {
super.onResume();
Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
mNfcAdapter.enableForegroundDispatch(activity, pendingIntent, null, null);
}
on the onPause method disable the Dispatch
#Override
protected void onPause() {
mNfcAdapter.disableForegroundDispatch(activity);
}
finally override the onNewIntent method to handle the intent
#Override
protected void onNewIntent(Intent intent) {
//handle intent here
}
in your case call "resolveIntent(intent)" inside the onNewIntent method
than follow this post to handle the IsoDep tag: Read data from NFC tag (IsoDep)
Related
Good evening community, I am reaching with the hopes of being educated about the following problem.
My intention with this code is to be able to handle USB permission intents in a receiver registered in a manifest file. The receiver gets USB Attached and detached actions, but not USB permissions when the user either accepts or declines the prompt.
Here is the code for the manifest, receiver and an activity to send the permissions request to the USB manager. And Finally, my target SDK is 28.
Any help is very much appreciated. Thank you very much.
public class BroadcastReceiver extends android.content.BroadcastReceiver{
public static final String USB_DEVICE_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
public static final String USB_DEVICE_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";
public static final String USB_PERMISSION ="com.android.example.USB_PERMISSION";
#Override
public void onReceive(Context context, Intent intent) {
Context applicationContext = context.getApplicationContext();
try{
if (intent != null) {
String action = intent.getAction();
if (!TextUtils.isEmpty(action)) {
if (action.equals(USB_DEVICE_ATTACHED) || action.equals(USB_PERMISSION)){
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
UsbManager usbManager = (UsbManager) applicationContext.getSystemService(Context.USB_SERVICE);
if (action.equals(USB_DEVICE_ATTACHED)){
if (!usbManager.hasPermission(device)){
intent.setAction(USB_PERMISSION);
intent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
intent.setClass(applicationContext, PermissionActivity.class);
applicationContext.startActivity(intent);
Toast.makeText(applicationContext, "Device Attached.", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(applicationContext, "Permissions already assigned", Toast.LENGTH_LONG).show();
}
}
else if (action.equals(USB_PERMISSION)){
if (usbManager.hasPermission(device)){
Toast.makeText(applicationContext, "USB Permissions are granted.", Toast.LENGTH_LONG).show();
}
}
}
else if (action.equals(USB_DEVICE_DETACHED)) {
Toast.makeText(applicationContext, "Device Detached.", Toast.LENGTH_LONG).show();
}
}
}
}
catch(Exception e){
Toast.makeText(applicationContext, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
Here is the activity:
public class PermissionActivity extends android.support.v7.app.AppCompatActivity {
public static final String USB_PERMISSION ="com.android.example.USB_PERMISSION";
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Context applicationContext = this.getApplicationContext();
Intent intent = getIntent();
if (intent != null )
{
if (intent.getAction().equals(USB_PERMISSION)){
if (!intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false )) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null) {
UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(applicationContext, 0, new Intent(USB_PERMISSION), 0);
mUsbManager.requestPermission(device, mPermissionIntent);
Toast.makeText(applicationContext, "Requesting Permission", Toast.LENGTH_LONG).show();
}
}
}
}
finish();
}
#Override
protected void onResume() {
super.onResume();
}
}
And finally, the manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.usbtest">
<uses-feature android:name="android.hardware.usb.host"/>
<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">
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PermissionActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize"
android:excludeFromRecents="true"
android:exported="true"
android:noHistory="true"
android:process=":UsbEventReceiverActivityProcess"
android:taskAffinity="com.example.taskAffinityUsbEventReceiver"
android:theme="#style/Theme.AppCompat.Translucent">
<intent-filter>
<action android:name="com.android.example.USB_PERMISSION"/>
</intent-filter>
</activity>
<receiver android:name=".BroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"/>
<action android:name="com.android.example.USB_PERMISSION"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
I found the problem. Since Android 8.0, there are more restrictions with manifest-declared
broadcast receivers and the type of actions that can be received. The USB Permissions action is not part of the limited list of actions that can be received. Here are some links regarding this issue.
https://developer.android.com/guide/components/broadcasts#context-registered-recievers
https://developer.android.com/guide/components/broadcast-exceptions
Android launch application multiple times on an NFC tag discovered. I tried using the answer in this link Android launch activity multiple times on an NFC tag. but it says resources not found.
I am reading the tag from a fragment. Any help.
This is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.atsl.touch">
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-feature android:name="android.hardware.nfc" android:required="true"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:launchMode= "singleInstance"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
this is my fragments onResume()
public void onResume() {
super.onResume();
Intent intent =getActivity().getIntent();
pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, 0);
intentFilter = new IntentFilter[] { };
nfcAdapter = NfcAdapter.getDefaultAdapter(getContext());
String action = intent.getAction();
boolean cardMatched=false;
Tag tag=null;
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
Toast.makeText(getContext(),"Blahhhhh",Toast.LENGTH_LONG).show();
tag = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
tappedCard=new Card();
tappedCard.setCardId(MainActivity.bytesToHexString(tag.getId()));
//tag = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String[] techList = tag.getTechList();
////////////+___________________________________________
////////////+___________________________________________
if(tag == null){
Toast.makeText(getContext(), "tag == null",Toast.LENGTH_SHORT).show();
}else{
Card cc=new Card();
String tagId= MainActivity.bytesToHexString(tag.getId());
cc.setCardId(tagId);
dbHelper=new DBHelper(getContext());
ArrayList<Card> checkList=dbHelper.getAllCards();
if(checkList.size()>0)
{
for(Card d:checkList)
{
if (cc.equals(d))
{
cardMatched=true;
Toast.makeText(getContext(), "Card Matched !!!!",Toast.LENGTH_SHORT).show();
showCardDetails(cc.getCardId());
break;
}
else
{
continue;
}
}
if (!cardMatched)
{
imgBusCard.setVisibility(View.GONE);
showDetails.setVisibility(View.VISIBLE);
Toast.makeText(getContext(), "Card Not Matched !!!!",Toast.LENGTH_SHORT).show();
CustomDialogAddCard customDialogAddCard =new CustomDialogAddCard(getActivity(),"T",cc);
customDialogAddCard.show();
showCardDetails(cc.getCardId());
}
}
else {
Toast.makeText(getContext(), "No cards found !!!!",Toast.LENGTH_SHORT).show();
CustomDialogAddCard customDialogAddCard =new CustomDialogAddCard(getActivity(),"T",cc);
customDialogAddCard.show();
showCardDetails(cc.getCardId());
}
}
}else{
Toast.makeText(getContext(),
"onResume() : " + action,
Toast.LENGTH_SHORT).show();
}
}
The problem is when i run the application and hold an nfc tag near the phone app starts a new instance of the application. it doesn't detect the tag in the initial instance.
This question already has answers here:
How to use NFC ACTIONS
(5 answers)
Closed 3 years ago.
I'm creating application that do some action if hit NFC chip , I know the following information about NFC in Android please correct me if I am wrong
you can't register actions of the NFC to receiver in the manifest file , only activities .
each NFC chip has it's own unique id .
what I want to do is
while the Application is in the background or it's closed , if I hit NFC chip with id (ex 1234) my app lunched and do some action.
is that possible ?
if yes, how it can be achieved?
edit
here is my code , it does open when you check any NFC chip and get this action android.nfc.action.TECH_DISCOVERED
but when it open the action is android.intent.action.MAIN
MainActivity.java
public class MainActivity extends AppCompatActivity {
public static final String ACTION_CHECK_IN= "checked";
public static final String ACTION_CHECK_OUT= "unchecked";
private NfcAdapter mNfcAdapter;
boolean isCheckedIn = false;
private static final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
isCheckedIn = Pref.getBoolean("check", false);
}
#Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume() called with: " + "");
String action = getIntent().getAction();
if (action != null) {
Log.d("MainActivity", "onCreate(" + action + ")");
if (action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED) || action.equals(NfcAdapter.ACTION_TAG_DISCOVERED) || action.equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
if (!isCheckedIn) {
isCheckedIn = true;
Pref.putBoolean("check", true);
Log.d("checking","IN");
} else {
isCheckedIn = false;
Pref.putBoolean("check", false);
Log.d("checking","Out");
}
}
}
if (!mNfcAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "Please open it ", Toast.LENGTH_SHORT).show();
}
/**
* It's important, that the activity is in the foreground.
* Otherwise an IllegalStateException is thrown.
*/
setupForegroundDispatch(this, mNfcAdapter);
}
#Override
protected void onPause() {
/**
* Call this before onPause, otherwise an IllegalArgumentException is thrown.
*/
stopForegroundDispatch(this, mNfcAdapter);
super.onPause();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
public static void setupForegroundDispatch(Activity activity, NfcAdapter adapter) {
final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent pendingIntent = PendingIntent.getActivity(
activity.getApplicationContext(), 0, intent, 0);
IntentFilter[] filters = new IntentFilter[2];
String[][] techList = new String[][]{};
// same filter as manifest - action, category
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
filters[1] = new IntentFilter();
filters[1].addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}
public static void stopForegroundDispatch(Activity activity, NfcAdapter adapter) {
adapter.disableForegroundDispatch(activity);
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.srjo.pocnfcadapter">
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<application
android:name=".MyApplication"
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>
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="#xml/filter_nfc" />
</activity>
</application>
</manifest>
According the the Android Developer site, it is possible for your app to filter NFC intents such as ACTION_NDEF_DISCOVERED.
First register your app in the manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.survivingwithandroid.nfc" >
....
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain"/>
</intent-filter>
<manifest>
Then you should use foreground dispatch (foreground app). Finally you use NFCAdapter to read the content (onNewIntent).
I wrote a detailed posts about NFC if you like give a look.
I am creating an app where two devices communicate over NFC and then one transmits to the server.
I am trying to develop this for both pre- and post-lollipop. The problem is that I create my NDEF message in one app and then receive it in another app. However when I try to receive in the other app, the NFC "New Tag collected" screen is opened instead of my app. This is a real problem for me, I'm just sending a string which I need to send to a web service.
Below is the manifest of the receiving application:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.devcompany.paymentcustomer" >
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".activities.HomeActivity"
android:launchMode="singleTop"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is my code for sending:
mNfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
//If device is running lollipop remove the touch to beam action
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
mNfcAdapter.setNdefPushMessageCallback(nDefCallback, getActivity());
mNfcAdapter.invokeBeam(getActivity());
}
}, 2000);
}else{
//leave touch to beam action
mNfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {
#Override
public NdefMessage createNdefMessage(NfcEvent event) {
NdefMessage message = new NdefMessage((new NdefRecord[]{createMime("application/com.devcompany.paymentvendor.fragments", mToBeam.getBytes()) }));
return message;
}
}, getActivity());
mNfcAdapter.setOnNdefPushCompleteCallback(
new NfcAdapter.OnNdefPushCompleteCallback() {
#Override
public void onNdefPushComplete(NfcEvent event) {
}
}, getActivity());
}
Here is my code for receiving:
public class HomeActivity extends ActionBarActivity {
private NfcAdapter mAdapter;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_layout);
mAdapter = NfcAdapter.getDefaultAdapter(this);
textView = (TextView)findViewById(R.id.fortressLabel);
}
#Override
public void onResume(){
super.onResume();
if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())){
processIntent(getIntent());
}
}
#Override
public void onNewIntent(Intent intent) {
// onResume gets called after this to handle the intent
setIntent(intent);
}
private void processIntent(Intent intent){
//textView.setText(intent.getDataString());
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
// only one message sent during the beam
NdefMessage msg = (NdefMessage) rawMsgs[0];
Log.i(this.getLocalClassName(), intent.getDataString());
}
}
I have debugged my receiving code and stepped through it, when I hold the phones together, the code doesn't even enter the onResume method. The tags intent is launched first. I don't understand why this is happening. Can anyone help me on this one?
You are sending a MIME type record of type "application/com.devcompany.paymentvendor.fragments":
NdefMessage message = new NdefMessage(new NdefRecord[] {
NdefRecord.createMime("application/com.devcompany.paymentvendor.fragments",
mToBeam.getBytes())
});
Consequently, you also need to instruct your receiving activity to actually receive that MIME type record. You can register your app to open (and receive) upon detection of that MIME type in an NFC event by adding an NDEF_DISCOVERED intent filter for your activity:
<activity android:name=".activities.HomeActivity" ...>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/com.devcompany.paymentvendor.fragments" />
</intent-filter>
</activity>
I am having difficulty getting a BroadcastReceiver to process my IntentService response. The service processes multiple different actions and returns an action type. The receiver will never seem to pick it up, however. The intent does get called as I can debug and set a breakpoint in the IntentService and see the action get processed successfully. I just never see the textbox updated with the appropriate data or see the BroadcastReceiver evein being called.
IntentService
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
// Data the service was called with.
Bundle incomingData = intent.getExtras();
String key = incomingData.getString(KEY_APPKEY);
String secret = incomingData.getString(KEY_SECRET);
String collection = incomingData.getString(KEY_COLLECTION);
CheckinManager cm = new CheckinManager(this.getApplicationContext(),key,secret,collection);
Intent broadcastIntent = new Intent();
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
if (action == ACTION_GET_POI) {
Double lat = incomingData.getDouble(KEY_LATITUDE);
Double lon = incomingData.getDouble(KEY_LONGITUDE);
ArrayList<POI> nearbyPOIs = new ArrayList<POI>();
//broadcastIntent.setAction(ACTION_GET_POI_PROCESSED);
broadcastIntent.setAction("com.msalinger.checkinmanager.CheckinService.getPOIProcessed");
try {
nearbyPOIs = cm.getPOI(lat, lon);
broadcastIntent.putExtra(OUT_KEY_RESULT, true);
broadcastIntent.putExtra(OUT_KEY_ERROR, "");
broadcastIntent.putParcelableArrayListExtra(OUT_KEY_POILIST, nearbyPOIs);
} catch (JSONException ex) {
Log.d(TAG,ex.getMessage() + "\n" + ex.getStackTrace());
broadcastIntent.putExtra(OUT_KEY_RESULT, false);
broadcastIntent.putExtra(OUT_KEY_ERROR, ex.getMessage());
}
}
else if (action == ACTION_CHECK_IN) {
// Do something
}
else if (action == ACTION_GET_CHECKINS) {
// Do Something
}
else if (action == ACTION_FIND_NEARBY_POIS_WITH_CHECKINS) {
// Do Something
}
sendBroadcast(broadcastIntent);
}
Broadcast Receiver as sub-class of Main Activity
public class CheckinReceiver extends BroadcastReceiver {
private final static String INTENT_BASE_URI = "com.msalinger.checkinmanager.CheckinService";
private final static String ACTION_GET_POI_PROCESSED = ".getPOIProcessed";
private final static String ACTION_CHECK_IN_PROCESSED = ".checkInProcessed";
private final static String ACTION_GET_CHECKINS_PROCESSED = ".getCheckinsProcessed";
private final static String ACTION_FIND_NEARBY_POIS_WITH_CHECKINS_PROCESSED = ".findNearbyPOIsWithCheckinsProcessed";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.msalinger.checkinmanager.CheckinService.getPOIProcessed")) {
tv = (TextView)findViewById(R.id.textBox1);
Bundle incomingData = intent.getExtras();
String st = "";
if (incomingData.getBoolean("result")) {
ArrayList<POI> poiList = incomingData.getParcelableArrayList("poList");
st = printPOI(poiList);
}
else {
st = incomingData.getString("error");
}
}
else if (intent.getAction().equals(INTENT_BASE_URI + ACTION_CHECK_IN_PROCESSED)) {
}
else if (intent.getAction().equals(INTENT_BASE_URI + ACTION_GET_CHECKINS_PROCESSED)) {
}
else if (intent.getAction().equals(INTENT_BASE_URI + ACTION_FIND_NEARBY_POIS_WITH_CHECKINS_PROCESSED)) {
}
}
}
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.msalinger.checkinmanagerdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:enabled="true"
android:name="com.msalinger.checkinmanager.CheckinService" />
<receiver
android:name=".CheckinReceiver">
<intent-filter>
<action android:name="com.msalinger.checkinmanager.CheckinService.getPOIProcessed" />
</intent-filter>
<intent-filter>
<action android:name="com.msalinger.checkinmanager.CheckinService.checkInProcessed" />
</intent-filter>
<intent-filter>
<action android:name="com.msalinger.checkinmanager.CheckinService.getCheckinsProcessed" />
</intent-filter>
<intent-filter>
<action android:name="com.msalinger.checkinmanager.CheckinService.findNearbyPOIsWithCheckinsProcessed" />
</intent-filter>
</receiver>
</application>
</manifest>
What am I doing wrong? Note that the IntentService exists as part of an Android class library with a different package than the Main activity.
Because the receiver exists to update data in the activity, it should be registered when the activity resume and unregistered when the activity pause. And it should not be in the manifest (see the doc for this).
If it's not the case, it shouldn't be a subclass of your activity.
In all cases, I think your Receiver is not called because it has not the right name in the manifest. It may be something like this : .MainActivity$CheckinReceiver.