我有一个BroadcastReceiver(),即使应用程序暂停/死亡,也会调用onReceive方法。我希望只有当用户从主屏幕打开通知时才会调用onReceive,我该怎么做?
MainClass:
private ObservableCollection<SalesPeriodV> salesPeriods = new ObservableCollection<SalesPeriodV>();
public ObservableCollection<SalesPeriodV> SalesPeriods
{
get { return salesPeriods; }
set { salesPeriods = value; OnPropertyChanged("SalesPeriods"); }
}
private SalesPeriodV selectedItem = new SalesPeriodV();
public SalesPeriodV SelectedItem
{
get { return selectedItem; }
set { selectedItem = value; OnPropertyChanged("SelectedItem"); }
}
private void _ComboBoxCurrencyExchange_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox cb = (ComboBox)sender;
SelectedItem = (SalesPeriodV)(cb.SelectedItem);
string text = cb.SelectedValue.ToString();
}
BroadCastReceiver类
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
final Intent intent = registerReceiver(broadcastReceiver, intentFilter);
清单
Intent intent;
if(rootActivity.getPackageName().equalsIgnoreCase("myapp"))
{
//your app is open
intent = new Intent();
intent.setComponent(rootActivity);
} else {
//your app is not open,start it by calling launcher activity
intent = new Intent(context, MainActivity.class);
}
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);