AlarmManager或服务

编程入门 行业动态 更新时间:2024-10-09 19:17:21
本文介绍了AlarmManager或服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是一个C ++开发,开发我的第一个Android应用程序。我的应用程序是一个特殊的提醒的。我在寻找做到这一点的最好办法。我曾尝试这个办法:

I'm a C++ developer and developing my first Android application. My application is an special kind of Reminder. I'm looking for the best way to do it. I have tried this approaches:

  • 使用服务
  • 使用的AlarmManager
  • 我的问题是,我可以使用AlarmManager单?它是一个CPU耗时的任务考虑到我的AlarmManager应该被解雇每隔1秒? (似乎每一个AlarmManager执行,除了主进程的新进程时创建,并立即被杀死)。

    My question is that can I use AlarmManager singly? Is it a CPU time consuming task considering that my AlarmManager should be fired every 1 second ? (It seems that every time an AlarmManager is executed a new process except main process is created and immediately is killed).

    如果我使用一个服务的话,我的应用程序应该永远留在记忆并且如果由用户杀死会发生什么!?

    If I use a service then my application should always stay in memory and also what happens if is killed by user !?

    如何安卓报警(默认安装的应用程序)的作品?

    How Android Alarms (default installed application) works?

    任何帮助将是AP preciated。

    Any help would be appreciated.

    推荐答案

    后的服务,它返回START_STICKY并使其startForeground,这样一来你的应用程序将运行所有即使系统杀死它使用资源的时间,而它会启动和正常再次运行以及有关用户杀死它做好,这甚至是大型应用抱怨像WhatsApp的为u在wornings安装了WhatsApp第一次的时候看到的东西。这里的服务应是这样一个例子:

    Use a Service that returns START_STICKY and make it startForeground, this way your app will run all the time even if System kills it for resources after a while it'll be up and running again normally, and about user killing it well this is something even big apps complain about like Whatsapp as u see in the wornings when installing whatsapp first time. here is an example of how the service should be like:

    public class Yourservice extends Service{ @Override public void onCreate() { super.onCreate(); // Oncreat called one time and used for general declarations like registering a broadcast receiver } @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); // here to show that your service is running foreground mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent bIntent = new Intent(this, Main.class); PendingIntent pbIntent = PendingIntent.getActivity(this, 0 , bIntent, Intent.FLAG_ACTIVITY_CLEAR_TOP); NotificationCompat.Builder bBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("title") .setContentText("sub title") .setAutoCancel(true) .setOngoing(true) .setContentIntent(pbIntent); barNotif = bBuilder.build(); this.startForeground(1, barNotif); // here the body of your service where you can arrange your reminders and send alerts return START_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onDestroy() { super.onDestroy(); stopForeground(true); } }

    这是最好的方案,持续的服务,以执行codeS。

    this is the best recipe for ongoing service to execute your codes.

    更多推荐

    AlarmManager或服务

    本文发布于:2023-11-27 11:04:02,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1637806.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:AlarmManager

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!