【问题标题】:Empty intent filter gives me problems空意图过滤器给我带来了问题
【发布时间】:2023-04-06 03:20:02
【问题描述】:

我有一个需要启动和停止活动的应用程序。

到目前为止,我们可以启动 Activity。

当我尝试停止 Activity 时出现问题。

这是广播意图关闭 Activity 的 AlarmManager:

        Intent ftue = new Intent(ctxt, VideoActivty.class);
        ftue.putExtra("finish", true);
        PendingIntent pftue = PendingIntent.getBroadcast(ctxt, 0, ftue, 0);
        Calendar calSet4 = Calendar.getInstance();
        calSet4.set(Calendar.MONTH, c.get(Calendar.MONTH));
        calSet4.set(Calendar.YEAR, c.get(Calendar.YEAR));
        calSet4.set(Calendar.DAY_OF_WEEK, 3);
        calSet4.set(Calendar.HOUR_OF_DAY, hftue);
        calSet4.set(Calendar.MINUTE, mftue);
        calSet4.set(Calendar.SECOND, 0);
        calSet4.set(Calendar.MILLISECOND, 0);

        //calSet.setTimeZone(TimeZone.getTimeZone("UTC"));
        mgr.setRepeating(AlarmManager.RTC_WAKEUP, calSet4.getTimeInMillis(),
                7 * 24 * 60 * 60 * 1000, pftue);

在我的 Activty 中,我实现了一个 BroadcastReceiver,它应该关闭 Activty。

@Override
public void onResume() {
super.onResume();
IntentFilter f=new IntentFilter();
registerReceiver(receiver, f);
}


@Override
public void onPause() {
unregisterReceiver(receiver);
super.onPause();
}

BroadcastReceiver receiver=new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        Log.e("","intento ricevuto");
        if(intent.getBooleanExtra("finish",false))finish();

    }
};

我的应用程序没有收到广播的意图,我知道这是因为意图过滤器是空的。

请问我应该如何实现意图过滤器来接收广播?

谢谢!

【问题讨论】:

  • 另外,如果你的 Activity 被暂停,它不会收到广播。

标签: android intentfilter


【解决方案1】:

为什么你的意图过滤器是空白的?您可以将意图操作编写为任何字符串(但 SDK 操作中不应存在)

//喜欢

IntentFilter f = new IntentFilter("com.android.INTENT_ACTION_TO_CLOSE_ACTIVITY");

并使用

Intent mIntent = new Intent("com.android.INTENT_ACTION_TO_CLOSE_ACTIVITY");
sendBroadcast(mIntent);

在条件下,当您要关闭 Activity 时。

【讨论】:

  • 我可以知道你发生了什么事吗?
  • Kumarn 不好意思,我测试的时候以为不行,但是我发现了问题并修复了。非常感谢Pankaj !!!!!!
猜你喜欢
  • 2011-05-22
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 2011-07-27
  • 2014-03-02
  • 1970-01-01
相关资源
最近更新 更多