这个问题可能看起来有点宽泛,但我敢肯定,一旦你阅读了,上下文就会变得清晰。
我需要编写一个不扩展活动的类,但我希望它能够运行 onNewIntent 方法。
这样做的原因是因为该类实际上没有或不需要接口或连接到 UI。我只需要它在后台嗅探一些意图。我还不能让 onNewIntent 部分工作。
这就是我的意思。
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.
}
}
由于该类不需要接口,我认为它没有理由扩展活动。但是对于我想要正常工作的事情,我需要检查新的意图。
想知道是否有非活动类监听新意图的解决方法。