简体   繁体   English

如何在Android中同时运行2个Intent?

[英]How to run 2 intents in android at the same time?

I want to set two alarms in my app through intent.This is how the app should work, it should get time form the user and set alarm for that time +3 minutes.For instance, if the user wants to set an alarm for 8:30 it should set alarm for 8:30, and then for 8:33 immediately.I've used 3 as an example, but in the actual functioning, I will include a variable there.This app is to set multiple alarms with a gap that user enters. 我想通过意图在我的应用程序中设置两个警报。这是应用程序的工作方式,它应该从用户那里获得时间并为该时间设置+3分钟的警报,例如,如果用户想要为8个警报设置警报:30它将设置8:30的警报,然后立即设置8:33的警报。我以3为例,但在实际功能中,我将在其中包含一个变量。此应用程序将使用用户输入的差距。 It will prompt the user about the number of such alarms to set.So I wrote two intents for it but only first one is working.This is my code: 它会提示用户要设置的警报数量,因此我为此写了两个意图,但只有第一个意图起作用,这是我的代码:

Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);

    intent.putExtra(AlarmClock.EXTRA_MESSAGE, "Your alarm");
    intent.putExtra(AlarmClock.EXTRA_HOUR, result);
    intent.putExtra(AlarmClock.EXTRA_MINUTES, i);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
        } else {
          Toast.makeText(getApplicationContext(), "There is alarm option for your device", Toast.LENGTH_SHORT).show();
       }

    Intent intent1 = new Intent(AlarmClock.ACTION_SET_ALARM);

     intent1.putExtra(AlarmClock.EXTRA_MESSAGE, "Your alarm");
     intent1.putExtra(AlarmClock.EXTRA_HOUR, result);
     intent1.putExtra(AlarmClock.EXTRA_MINUTES, i+3);
     if (intent1.resolveActivity(getPackageManager()) != null) {
         startActivity(intent1);
        } else {
           Toast.makeText(getApplicationContext(), "There is alarm option for your device", Toast.LENGTH_SHORT).show();
        }

Can we make intent object as an array? 我们可以将意图对象作为数组吗?

Thanks You for the help sir. 谢谢您的帮助,先生。 Here is what I did. 这是我所做的。

 @Override
  public void onResume()
 { super.onResume();
   secondintent();
 }


public void secondintent() {
        if (test == 1)
        {
            Intent intent1 = new Intent(AlarmClock.ACTION_SET_ALARM);

        intent1.putExtra(AlarmClock.EXTRA_MESSAGE, mess);
        intent1.putExtra(AlarmClock.EXTRA_HOUR, result);
        intent1.putExtra(AlarmClock.EXTRA_MINUTES, i + 5);
        startActivity(intent1);
        i=i+5;
        }

You can not go back to your main or previous activity once you've performed startActivity(firstIntent) unless you'll finish() the next activity or you hit the back button. 执行startActivity(firstIntent) ,您将无法返回到主要活动或上一个活动,除非您将finish()下一个活动或单击“ back按钮。 Every time you go back to your previous activity, it's onResume() will be called and in order to continue setting up your next alarm, you have to invoke startActivity(nextIntent) again. 每次返回上一个活动时,都会调用onResume() ,为了继续设置下一个警报,您必须再次调用startActivity(nextIntent) Think of that startActivity(firstIntent) as a break statement for your method. startActivity(firstIntent)视为方法的break语句。

So what you can do is, try to set the next alarm from your onResume() . 因此,您可以做的是,尝试从onResume()设置下一个警报。

Read about Activity-lifecycle concepts here 在此处阅读有关活动生命周期概念的 信息

I think you should change your code use alarm manager for multiple alarm if i'm right than use 我认为如果我比使用正确,则应更改代码使用警报管理器来进行多个警报
This link 这个连结

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM