首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何通过定期从独立进程中的Android后台服务调用api来发送通知

如何通过定期从独立进程中的Android后台服务调用api来发送通知
EN

Stack Overflow用户
提问于 2016-05-06 11:25:29
回答 1查看 1.9K关注 0票数 0

我希望通过定期调用API来向用户发送通知,以检查是否有任何挂起通知,用户是否正在使用该应用程序。我想在后台工作24小时。

现在我正在由警报经理来完成这个任务

以下是代码:

服务:

代码语言:javascript
运行
复制
public class NotificationService extends IntentService { 

    public NotificationService() {

        super("NotificationService");

    }

    @Override

    protected void onHandleIntent(Intent intent) { 

        //call api

        sendNotification();

    } 
    private void sendNotification() {

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("Hello")
                        .setContentText("Hello World")
                        .setAutoCancel(true); 
        Intent resultIntent = new Intent(this, MainActivity.class);  
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
        mNotificationManager.notify(1, mBuilder.build());
        playNotificationSound();

    }
}

接收机

代码语言:javascript
运行
复制
public class NotificationServiceReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent dailyUpdater = new Intent(context, NotificationService.class);

        context.startService(dailyUpdater); 
    }
}

AlarmManager

代码语言:javascript
运行
复制
private void setRecurringAlarm(Context context) {

        Calendar updateTime = Calendar.getInstance();
        updateTime.setTimeZone(TimeZone.getDefault()); 
        Intent downloader = new Intent(context, NotificationServiceReceiver.class);
        downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 123546, downloader, PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), REPEAT_TIME, pendingIntent);


    }

问题是,警报管理器有时不工作,当我再次启动应用程序,然后它的统计工作。

EN

回答 1

Stack Overflow用户

发布于 2016-05-06 12:29:09

问题是您的BroadcastReceiver保证运行,但由于省电而不能运行Service。您需要使用一个唤醒锁(请参阅WakefulReceiver上的文档),以确保您的Service有机会运行。本文将提供帮助:http://hiqes.com/android-alarm-ins-outs/

注意,新的Doze模式也会影响到这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37071187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档