Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当PendingIntent被触发时,是否有方法杀死某个活动?_Android_Android Activity_Alarmmanager_Android Pendingintent - Fatal编程技术网

Android 当PendingIntent被触发时,是否有方法杀死某个活动?

Android 当PendingIntent被触发时,是否有方法杀死某个活动?,android,android-activity,alarmmanager,android-pendingintent,Android,Android Activity,Alarmmanager,Android Pendingintent,在Android中,要杀死一个活动,只需使用finish()。但是在我的例子中,我想在启动pendingent时终止活动 我的代码如下: Intent intent = new Intent(MainActivity.this, NextActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(Utils.KEY_RECORD_TIME, recordLength); PendingInte

在Android中,要杀死一个活动,只需使用
finish()
。但是在我的例子中,我想在启动
pendingent
时终止活动

我的代码如下:

Intent intent = new Intent(MainActivity.this, NextActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Utils.KEY_RECORD_TIME, recordLength);

PendingIntent pintent = PendingIntent.getActivity(MainActivity.this,
        piNumber, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent);
如果我在下面添加
finish()
,活动将在
AlarmManager
中设置的时间之前终止

我希望该活动在时间结束之前一直可见,并结束它。有没有办法做到这一点?我在Stack Overflow中进行了一些搜索,但没有找到答案。

请看。如果您可以作为单个top启动活动,那么您的实例应该通过该调用接收挂起的意图

如果无法使用singel top,并且正在启动活动的新实例,则可以使用共享静态来获取新实例的
onCreate()
finish()
中的原始实例。

查看。如果您可以作为单个top启动活动,那么您的实例应该通过该调用接收挂起的意图


如果无法使用singel top,并且正在启动活动的新实例,则可以使用共享静态在新实例的
onCreate()
finish()
中获取原始实例。

您可以在活动中动态注册。对于挂起的意图,您可以使用。因此,当报警管理器调用广播接收器时,您可以在广播接收器内调用
finish()
。请注意,如果不再需要接收器,则必须注销它。例如,可以在
onDestroy()
中执行此操作。

您可以在活动中动态注册接收器。对于挂起的意图,您可以使用。因此,当报警管理器调用广播接收器时,您可以在广播接收器内调用
finish()
。请注意,如果不再需要接收器,则必须注销它。例如,可以在
onDestroy()
中执行此操作。

在计时器中,可以获得挂起意图的时间延迟

    new Timer().schedule(new TimerTask() {

    @Override
    public void run() {
        // TODO Auto-generated method stub

    }
},pending intent delay);

请试试这个。它对我有用。

在一个计时器中,你可以得到等待意图的时间延迟

    new Timer().schedule(new TimerTask() {

    @Override
    public void run() {
        // TODO Auto-generated method stub

    }
},pending intent delay);

请试试这个。它对我有用

请解释你的
活动
的作用。您可能不应该使用
AlarmManager
,而且您也无法控制用户使用
BACK
按钮来完成
活动。如果您想在特定时间完成活动,请调用计时器任务。并在其中调用finish。@Squonk我必须在特定时间捕获视频(例如12h00、13h00、15h00等)。当应用程序捕获视频2分钟后,我希望它返回到预览活动(MainActivity.java)。我不愿意先使用AlarmManager,但经过几天的研究,我意识到我没有其他选择。如果你知道更好的方法,请与我分享。@S谢谢!这是我没有想到的一种方法。请解释你的
活动的功能。你可能不应该使用
AlarmManager
,你也没有任何控制权ol超过用户使用
BACK
按钮完成
活动
。如果您想在特定时间完成活动,请调用计时器任务。并在其内部调用finish。@Squonk我必须在特定时间捕获视频(例如12h00、13h00、15h00等)。当应用程序捕获视频2分钟后,我希望它返回预览活动(MainActivity.java)。我不愿意先使用AlarmManager,但经过日复一日的研究,我意识到我没有其他选择。如果你知道更好的方法,请分享。@SREEJITH谢谢!这是我没有想到的一种方法。谢谢!似乎设置为Intent.FLAG\u ACTIVITY\u SINGLE\u TOP就可以了。好的,经过一些测试,我发现FLAG\u ACTIVITY\u SING\uLE_TOP没有清除堆栈中的活动。谢谢!似乎设置为Intent.FLAG_ACTIVITY_SINGLE_TOP是为了完成任务。好的,经过一些测试后,我发现FLAG_ACTIVITY_SINGLE_TOP没有清除堆栈中的活动。cal.getTimeInMillis()是什么在你的alarm.set方法中?我在问了一个愚蠢的问题后才发现。我正在尝试你的解决方案。你的alarm.set方法中的cal.getTimeInMillis()是什么?我在问了一个愚蠢的问题后发现了。我正在尝试你的解决方案。