Java IntentService不工作

Java IntentService不工作,java,android,broadcastreceiver,intentservice,Java,Android,Broadcastreceiver,Intentservice,我正在构建一个应用程序,根据该应用程序,通知将在特定时间响起,然后如果无人看管15分钟,通知将消失。当我插入设备并运行代码时,它就会工作。但是,一旦我拔下设备并运行应用程序,通知就会起作用,但如果无人看管,15分钟后通知不会消失。请建议我如何运行应用程序,就像将设备插入计算机时一样。此外,当应用程序被终止时,它应该可以工作 仅供参考,我正在使用通知、alarmmanager、广播接收器和intentservice。下面是我的代码片段 AlarmReceiver.java public class

我正在构建一个应用程序,根据该应用程序,通知将在特定时间响起,然后如果无人看管15分钟,通知将消失。当我插入设备并运行代码时,它就会工作。但是,一旦我拔下设备并运行应用程序,通知就会起作用,但如果无人看管,15分钟后通知不会消失。请建议我如何运行应用程序,就像将设备插入计算机时一样。此外,当应用程序被终止时,它应该可以工作

仅供参考,我正在使用通知、alarmmanager、广播接收器和intentservice。下面是我的代码片段

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

    Notification(context, "Wifi Connection On");
    Intent background = new Intent(context, BackgroundService.class);
    context.startService(background);
}

public void Notification(final Context context, String message) {
// notification codes 

    }

}
BackgroundService.java

public class BackgroundService extends IntentService {

public BackgroundService() {
    super("BackgroundService");
}


@Override
protected void onHandleIntent(Intent intent) {
   //countdown 15 minutes and cancel notification automatically 
    Timer timer=new Timer();
    TimerTask task=new TimerTask() {

        @Override
        public void run() {
            // Create Notification Manager
            NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            // Dismiss Notification
            notificationmanager.cancelAll();

        }
    };

    timer.schedule(task, 900000);

}
}
Manifest.xml

<receiver android:name=".AlarmReceiver" android:process=":remote" />
<service android:name=".BackgroundService" />

请给我一些建议。多谢各位

服务仅在CPU唤醒时运行。若CPU关闭,服务将不会运行

所以,若手机进入睡眠状态,要使您的服务正常运行,您需要获得唤醒锁

背景服务类

如果这确实可行,请尝试在Android清单文件中授予以下权限

<uses-permission android:name="android.permission.WAKE_LOCK" />

此服务将运行两次:第一次除重新安排外不执行任何操作,第二次取消通知

public class BackgroundService extends IntentService {
    private static final int REQUEST_CODE = 42;
    private static final String ACTION_CANCEL_NOTIFS = "CancelNotifications";

    public BackgroundService() {
        super("BackgroundService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null && ACTION_CANCEL_NOTIFS.equals(intent.getAction())) {
            NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationmanager.cancelAll();
        }
        else {
            reschedule();
        }
    }

    private void reschedule() {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.MINUTE, 15);

        final Intent serviceIntent = new Intent(this, getClass());
        serviceIntent.setAction(ACTION_CANCEL_NOTIFS);
        PendingIntent pendingIntent = PendingIntent.getService(this, REQUEST_CODE, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }
}
说明:

在您的代码中,我假设您使用startServiceNewIntentThis,BackgroundService.class启动服务。此意图在onHandleIntentIntent中作为参数传递,这意味着您可以从服务内部访问它

Intent允许您传递其他数据,例如对IntentFilter或Extra有用的操作。因为您还没有设置任何,所以执行的第一次转到onHandleIntent方法的else分支。AlarmManager计划在15分钟内以serviceIntent运行您的服务。注意serviceIntent.setActionACTION\u CANCEL\u NOTIFS。因此,执行的第二次转到if分支并取消通知

public class BackgroundService extends IntentService {
    private static final int REQUEST_CODE = 42;
    private static final String ACTION_CANCEL_NOTIFS = "CancelNotifications";

    public BackgroundService() {
        super("BackgroundService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null && ACTION_CANCEL_NOTIFS.equals(intent.getAction())) {
            NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationmanager.cancelAll();
        }
        else {
            reschedule();
        }
    }

    private void reschedule() {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.MINUTE, 15);

        final Intent serviceIntent = new Intent(this, getClass());
        serviceIntent.setAction(ACTION_CANCEL_NOTIFS);
        PendingIntent pendingIntent = PendingIntent.getService(this, REQUEST_CODE, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }
}

更好的方法是从活动内部创建挂起的意图,而不是使用startService启动服务。这将使您的服务更简单、更有凝聚力。

尝试使用AlarmManager而不是计时器。一旦你的服务完成处理意图,它将与你的计时器一起销毁。嗨,我是个初学者。您有没有关于如何在intent service中实现AlarmManager的示例,因为我的mainactivity中已经有了一个@你能告诉我Log的异常情况吗?BackgroundService第24行有一个错误。指的是PowerManager pm=PowerManager getSystemServiceContext.POWER\u服务;我对代码做了细微的修改。将完全锁定更改为部分锁定。试试这个。另外,请导入必要的程序包什么是必要的程序包?android.os.PowerManager、android.os.PowerManager.WakeLock无论何时出现,它都会立即删除通知,而不是15分钟后。每次运行时,它都会取消通知并重新安排自己的时间。这就是我留下评论的原因。只需在intent中传递一些附加数据并检查它是否在那里。我不需要它自己重新安排时间,但我需要类似于每次服务启动时,它会在当前时间的基础上增加15分钟,如果在15分钟内无人值守,它会自动删除通知。如我所说,只需传递一些附加数据,以区分您想要安排服务的情况和您想要实际取消通知的情况。我已经更新了答案。更好的设计是从您的活动中创建一个待定的意图。谢谢!成功了。你介意向我解释一下,如果你有意图null&&ACTION\u CANCEL\u NOTIFS.equalintent.getAction{表示什么意思?谢谢!