java - IntentService 不工作

标签 java android broadcastreceiver intentservice

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

仅供引用,我正在使用通知、警报管理器、广播接收器和 Intent 服务。下面是我的代码 fragment 。

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" />

请给我一些建议。谢谢。

最佳答案

该服务将运行两次:第一次除了重新安排之外什么都不做,第二次则取消通知。

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(new Intent(this, BackgroundService.class)) 启动服务。此 Intent 作为 onHandleIntent(Intent) 中的参数传递,这意味着您可以从服务内部访问它。

Intent 允许您传递附加数据,例如操作(对于 IntentFilter 很有用)或其他数据。由于您尚未设置任何内容,因此第一次执行将转到 onHandleIntent() 方法的 else 分支。然后,AlarmManager 计划在 15 分钟内使用 serviceIntent 运行您的服务。请注意serviceIntent.setAction(ACTION_CANCEL_NOTIFS)。因此,第二次执行时将转到 if 分支并取消通知。

更好的方法是直接从 Activity 内部创建待处理 Intent ,而不是使用 startService 启动服务。这将使您的服务更简单、更有凝聚力。

关于java - IntentService 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34694204/

相关文章:

android - 除非应用程序正在运行,否则应用程序不会响应收到的短信。

android - 无法接收 android.provider.Telephony.WAP_PUSH_RECEIVED

java - 在 SQL 中显示未减去的表

android - 在Android上读取RFID标签并在网页中插入数据

java - 你能把一个流分成两个流吗?

java - 如何在Android中使用Settings.Global类

android - Toast 背景更改以匹配 Activity 的主题

android - 在同一 fragment 的不同实例之间进行通信

java - 如何预测它是否会给出运行时错误或不会编译

java - 创建错误条件并将其传回客户端