0

this question may seem kinda broad but i'm sure once you've read up, the context will become clear.

I need to write a class that doesn't extend activity but i want it to be able to run onNewIntent Method.

The reason for this is because the class doesn't really have or need an interface or connect to a UI. I just need it to sniff for some intents in the background. I can't get the onNewIntent part to work just yet.

Here is what i mean.

public class MyClass {

  private PendingIntent mPendingIntent;
  private IntentFilter[] mFilters;


  public MyClass(){
    this.activity = activity;
    doAction();
  }

  public void doAction(){
   mPendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, new Intent(activity.getApplicationContext(),getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        mFilters = new IntentFilter[]{
                new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED),
                new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED),
                new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED)};
  }


  //Now i want to do some action when there is a new intent but since it doesn't extend activity, this don't work. Extending activity will mean using onCreate and all that other good stuff which i don't want
  @Override
  public void onNewIntent(Intent intent) { 
     Toast.makeText(activity.getApplicationContext(), "Got to intent", Toast.LENGTH_SHORT).show();
     super.onNewIntent(intent);
     doStuffWithIntent(intent);

    //This block doesn't work. I'm looking for alternative to get working. 
  }


}

Since the class doesn't need an interface, i see no reason why it should extend activity. But for what i want done to work properly, i need to check for new intent.

Would like to know if there is a workaround for a non activity class to listen for new intent.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • 1
    You seem to be adding unnecessary complexity to this. You just need an `Activity` without a UI? http://stackoverflow.com/a/2704801/4252352 – Mark Feb 08 '17 at 16:22
  • thanks at @MarkKeen this activity without a UI goes no where right? –  Feb 08 '17 at 16:35
  • I don't really understand what you mean by `goes no where` - you define what your Activity does? – Mark Feb 08 '17 at 16:38
  • i mean, will i be on the previous activity or will it open a blank one –  Feb 08 '17 at 16:40

2 Answers2

1

I just need it to sniff for some intents in the background

There are three "channels" for Intents: starting activities, starting and binding to services, and sending broadcasts.

The three Intent actions that you refer to in your code are used for starting activities. They will only be delivered to an activity, and you cannot "sniff" on those by any other means (short of perhaps rooting a device or building a custom ROM).

Would like to know if there is a workaround for a non activity class to listen for new intent.

Have the activity that is called with onNewIntent() forward the event to the non-activity class.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

another option would be to maintain a reference to an instance of your class in your activity, then call a public method on the class in the activity's onNewIntent() method

kira_codes
  • 1,457
  • 13
  • 38