Android计划在应用程序安装后在后台运行的任务

Android计划在应用程序安装后在后台运行的任务,android,service,repeat,Android,Service,Repeat,我正在尝试创建一个任务,以便每天凌晨1:00发送通知。即使应用程序关闭,任务也应运行 我使用服务和AlarmManager来完成这项工作。但问题是,只要应用程序启动,任务就会执行一次。我猜该服务因资源分配而被终止 我的问题是如何实现调度任务以保持工作并激活自身(即使它被终止),以及如何在安装应用程序时获取任务调度。目前,我把启动放在主要活动上 下面是代码片段:我非常感谢您在这方面提供的帮助: public class MyAlarmService extends Service{ p

我正在尝试创建一个任务,以便每天凌晨1:00发送通知。即使应用程序关闭,任务也应运行

我使用服务和AlarmManager来完成这项工作。但问题是,只要应用程序启动,任务就会执行一次。我猜该服务因资源分配而被终止

我的问题是如何实现调度任务以保持工作并激活自身(即使它被终止),以及如何在安装应用程序时获取任务调度。目前,我把启动放在主要活动上

下面是代码片段:我非常感谢您在这方面提供的帮助:

public class MyAlarmService extends Service{

     private NotificationManager mManager;

     @Override
     public IBinder onBind(Intent arg0){
       // TODO Auto-generated method stub
        return null;
     }

    @Override
    public void onCreate(){
       // TODO Auto-generated method stub  
       super.onCreate();
    }


    @Override
    public int onStartCommand(final Intent intent, final int flags,
            final int startId) {


       mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
       Intent intent1 = new Intent(this.getApplicationContext(),TabsFragmentActivity.class);
       intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

       PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,
                                                                PendingIntent.FLAG_UPDATE_CURRENT );
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        String shareBody = " Here is the share content body";
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);

       PendingIntent pendingIntent =PendingIntent.getActivity(this, 0,Intent.createChooser(sharingIntent, getResources().getString(R.string.choose_share_action)), 
                                                                PendingIntent.FLAG_UPDATE_CURRENT);


       String msgText = "Notification long text example!! "
                + "where you will see three different kind of notification. "
                + "you can even put the very long string here.";


       int icon = R.drawable.ic_launcher;

          Builder builder = new Notification.Builder(this);
          builder.setContentTitle(getResources().getString(R.string.noticifcation_content_title))
            .setContentText("This is the compact version")
            .setContentIntent(pendingNotificationIntent)
            .setTicker(getResources().getString(R.string.noticifcation_ticker))
            .setSmallIcon(icon)
            .setAutoCancel(false)
            .setPriority(Notification.PRIORITY_DEFAULT)
            .addAction(R.drawable.ic_action_share, 
                    getResources().getString(R.string.share),pendingIntent);
          Notification notification = new Notification.BigTextStyle(builder)
            .bigText(msgText).build();

          mManager.notify(0, notification);

    }
接收人:

public class MyReceiver extends BroadcastReceiver{

    @Override
     public void onReceive(Context context, Intent intent)  {

       Intent service = new Intent(context, MyAlarmService.class);
       context.startService(service);

     }
}


当应用程序启动时,要启动服务,您只需扩展
应用程序
类,然后在
onCreate
中启动服务

class MyApplication extends Application {

  @Override
  public void onCreate() {
    Intent service = new Intent(MyApplication.this, MyAlarmService.class);
    startService(service);
  }
}

您已经确定了执行一段代码的时间(间隔),最好使用AlarmManager,因为它更节能。 如果你的应用程序需要收听某种事件,那么服务就是你所需要的。 此代码每15分钟运行一次后台任务,即使应用程序未运行

public static void registerAlarm(Context context) {
     Intent i = new Intent(context, YOURBROADCASTRECIEVER.class);

     PendingIntent sender = PendingIntent.getBroadcast(context,REQUEST_CODE, i, 0);

     // We want the alarm to go off 3 seconds from now.
     long firstTime = SystemClock.elapsedRealtime();
     firstTime += 3 * 1000;//start 3 seconds after first register.

     // Schedule the alarm!
     AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
     am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
                     600000, sender);//10min interval
}您必须实现接收器

当你们的应用程序第一次启动服务时,若服务已经在运行,那个么什么也不做


您也可以使用其他操作来启动或不启动media scanner,这将允许我在安装应用程序后启动服务,但是每天运行的计划如何,它仍处于主要活动中,我是否必须将其更改为?如何注册?您可以始终在
AlarmManager
中注册此服务,您在何处添加此方法。。。目前我在主要活动中有一个,我认为这不是最好的解决方案。我想我需要把你的解决方案和“thepoosh”的解决方案结合起来。。。我试试看。你知道答案了吗?我也有同样的问题:
public static void registerAlarm(Context context) {
     Intent i = new Intent(context, YOURBROADCASTRECIEVER.class);

     PendingIntent sender = PendingIntent.getBroadcast(context,REQUEST_CODE, i, 0);

     // We want the alarm to go off 3 seconds from now.
     long firstTime = SystemClock.elapsedRealtime();
     firstTime += 3 * 1000;//start 3 seconds after first register.

     // Schedule the alarm!
     AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
     am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
                     600000, sender);//10min interval