Android 警报服务没有响应

发布于 2024-11-09 09:46:17 字数 2290 浏览 0 评论 0原文

我从 SO 以及其他一些网站的一些问题中获取了一些代码,并提出了一个合理的解决方案。

我想要做什么:我需要在 2 分钟不活动后关闭应用程序。因此,我们的想法是,当我们的应用程序进入“onPause”时启动警报服务,并在我们的应用程序进入“onResume”时取消该服务。

我当前拥有的内容:以下是当前正在运行的相关代码。我的问题是 TimeoutService java 文件永远不会被“onCreated”。简单地调用 AlarmManager.set 是否不会启动待处理的意图?

Timeout.java 文件

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class Timeout 
{
    private static final int REQUEST_ID = 0;
    private static final long DEFAULT_TIMEOUT = 2 * 60 * 1000;  // 2 minutes

    public static final String INTENT_TIMEOUT = "timeoutintent";

    public static void start(Context ctx) 
    {
        //long triggerTime = System.currentTimeMillis() + DEFAULT_TIMEOUT;
        long triggerTime = System.currentTimeMillis() + (5000);
        AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(ctx, TimeoutService.class);
        PendingIntent pi = PendingIntent.getBroadcast(ctx, REQUEST_ID, intent, 0);
        am.set(AlarmManager.RTC, triggerTime, pi);
    }

    public static void cancel(Context ctx) 
    {
        AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(ctx, TimeoutService.class);
        PendingIntent pi = PendingIntent.getBroadcast(ctx, REQUEST_ID, intent, 0);
        am.cancel(pi);
    }

}

LockingActivity 文件。它用作我所有活动的超类。

import android.app.Activity;
import android.widget.Toast;

public class LockingActivity extends Activity
{
    @Override
    protected void onPause() 
    {
        super.onPause();
        Timeout.start(this);
    }

    @Override
    protected void onResume() 
    {
        super.onResume();
        Timeout.cancel(this);
        checkShutdown();
    }

    private void checkShutdown() 
    {
        if ( AppVM.isShutDown() )
        {
            Toast.makeText(this, "Shuting Down", 1).show();
            finish();
        }
    }
}

我也可以发送 TimeoutService 文件,但它只是一个典型的服务文件。问题是 TimeoutService 类从未被实例化,所以我无法想象问题出在该类中。

I took some code from some questions here in SO as well as some other website and I came up with a reasonable solution.

What I am trying to do: I need to shutdown the app after 2 minutes of inactivity. So The idea is to start up the alarm service when our app goes in into 'onPause' and cancel the service when our app goes into 'onResume'.

What I currently Have: Here is the relevant code that is currently running. My issue is that the TimeoutService java file is never being 'onCreated'. Does simply calling AlarmManager.set NOT start up the pending intent?

The Timeout.java File

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class Timeout 
{
    private static final int REQUEST_ID = 0;
    private static final long DEFAULT_TIMEOUT = 2 * 60 * 1000;  // 2 minutes

    public static final String INTENT_TIMEOUT = "timeoutintent";

    public static void start(Context ctx) 
    {
        //long triggerTime = System.currentTimeMillis() + DEFAULT_TIMEOUT;
        long triggerTime = System.currentTimeMillis() + (5000);
        AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(ctx, TimeoutService.class);
        PendingIntent pi = PendingIntent.getBroadcast(ctx, REQUEST_ID, intent, 0);
        am.set(AlarmManager.RTC, triggerTime, pi);
    }

    public static void cancel(Context ctx) 
    {
        AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(ctx, TimeoutService.class);
        PendingIntent pi = PendingIntent.getBroadcast(ctx, REQUEST_ID, intent, 0);
        am.cancel(pi);
    }

}

LockingActivity File. This is used as a superclass to all of my Activities.

import android.app.Activity;
import android.widget.Toast;

public class LockingActivity extends Activity
{
    @Override
    protected void onPause() 
    {
        super.onPause();
        Timeout.start(this);
    }

    @Override
    protected void onResume() 
    {
        super.onResume();
        Timeout.cancel(this);
        checkShutdown();
    }

    private void checkShutdown() 
    {
        if ( AppVM.isShutDown() )
        {
            Toast.makeText(this, "Shuting Down", 1).show();
            finish();
        }
    }
}

I could send over the TimeoutService file as well, but it's just a typical service file. The problem is the TimeoutService class is never being instanced, so I can't imagine the problem would lie in that class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失眠症患者 2024-11-16 09:46:17

我认为你用闹钟让事情变得复杂了。使用 Handlerpostdelayed() 在两分钟内设置 Runnable,所有这些都在您的主要活动中进行。任何用户交互都会取消该帖子并在接下来的两分钟内设置一个新帖子。可运行仅需要 yourActivity.finish();

请在此处遵循此答案:计数 45 秒,暂停 20 秒,然后使用不同的标题重复,了解计时器以及如何删除回调的示例。

I think you are complicating things with an alarm. Use a Handler and postdelayed() to set a Runnable in two minutes, all in your main activity. Any user interaction cancels the post and sets a new one for the next two minutes. The runnable needs only yourActivity.finish();

Follow this answer here: Count for 45 seconds, pause for 20 then repeat with different title for an example of a timer and how to remove the callbacks.

穿透光 2024-11-16 09:46:17

您好,尝试对您的待处理意图使用 PendingIntent.FLAG_CANCEL_CURRENT 标志。

 PendingIntent pi = PendingIntent.getBroadcast(ctx, REQUEST_ID, intent,PendingIntent.FLAG_CANCEL_CURRENT);

将警报管理器设置为 RTC_WAKEUP 以唤醒设备(如果设备在某个地方关闭)并进行检查。

有了这个,您需要注册广播接收器并在警报响起时通过某些操作进行火灾意图。将操作设置为与用于待处理意图的相同意图。
在接收器中启动您的服务。
当警报关闭时,您的广播接收器应该能够侦听此 Intent 触发的操作。

意图意图=新意图(SCH_ALARM_ACTION);
Intent.setClass(getBaseContext(), SchAlarmReciever.class);
registerReceiver(new SchAlarmReciever(), new IntentFilter(SCH_ALARM_ACTION));
PendingIntent pi = PendingIntent.getBroadcast(getBaseContext(),
0,
意图,
PendingIntent.FLAG_CANCEL_CURRENT);

Hi Try using PendingIntent.FLAG_CANCEL_CURRENT flag for your Pending Intent.

 PendingIntent pi = PendingIntent.getBroadcast(ctx, REQUEST_ID, intent,PendingIntent.FLAG_CANCEL_CURRENT);

Make Alarm manager as RTC_WAKEUP to wake up device if it is going off some where and check.

With this you need to register a broadcast receiver and fire intent with some action when alarm goes off. Set action to same intent you are using for pending intent.
Start your service in your receiver.
Your this broadcast receiver should be able to listen to action fired by this Intent when Alarm goes OFF.

Intent intent = new Intent(SCH_ALARM_ACTION);
intent.setClass(getBaseContext(), SchAlarmReciever.class);
registerReceiver(new SchAlarmReciever(), new IntentFilter(SCH_ALARM_ACTION));
PendingIntent pi = PendingIntent.getBroadcast(getBaseContext(),
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文