2

我试图运行它在后台运行,每20秒一个Android服务和用户的经纬度长数据发送到服务器进行查杀应用程序不重新启动跟踪。这是我第一次启动我的应用程序。现在,如果我点击主页按钮,它仍然在后台运行。但是,现在如果我使用主页按钮从应用程序列表中杀死我的应用程序。然后用启动器图标重新启动我的应用程序。现在服务不启动。我每20秒使用一次Alarm Manager来触发我的服务。但在重新启动时,我的闹钟已设置,但未在广播接收器上注册,因为未调用我的服务。 下面是我的代码: - MyFragment.java的onCreateView()我在哪里设置我的报警: -使用AlarmManager每20秒后,运行的是Android服务从应用列表

Intent alarm = new Intent(mContext, AlarmReceiver.class); 
    boolean alarmRunning = (PendingIntent.getBroadcast(mContext, 0, alarm, PendingIntent.FLAG_NO_CREATE) != null); 
    if (alarmRunning == false) { 
     Log.e("In OnCreateView DDFrag", "AlarmRunning == False"); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, alarm, 0); 
     AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 20000, pendingIntent); 
    } else{ 
     Log.e("In OnCreateView DDFrag", "AlarmRunning == True"); 
    } 

AlarmReceiver.class: -

public class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent background = new Intent(context, MyService.class); 
     Log.e("AlarmReceiver", "Broadcasr Receiver started"); 
     context.startService(background); 
    } 
} 

MyService.class : -

public class MyService extends Service { 

    public boolean isServiceRunning; 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     this.isServiceRunning = false; 
    } 



    @Override 
    public void onDestroy() { 
     this.isServiceRunning = false; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     if(!this.isServiceRunning) { 
      sendDataToServer(); 
      this.isServiceRunning = true; 
     } 
     return START_STICKY; 
    } 


    private void sendDataToServer() { 
     // Performing my operation in this method.. 
    // On Success of the method performed I am calling the below method and setting the below variables: 
    stopSelf(); 
     this.isServiceRunning = false; 
    } 
} 

此外我定义我的服务和接收器在manifest.xml文件为: -

<service android:name="com.mypackagename.services.MyService" /> 

    <receiver android:name="com.mypackagename.services.AlarmReceiver" /> 

请帮我解决问题,或点我我在做什么wroung。 至于第一次。因为我的闹钟管理器没有设置,它会设置并在20秒后调用服务appropiatley。但是,如果我杀了我的应用程序并重新启动它,那么我的闹钟已设置为不启动或重新设置。现在我的AlarmReceiver类永远不会收到Alarm BroadcastReceiver。

+0

你测试什么设备? –

+0

@DavidWasser我对联想A6010插槽 –

+0

测试你有没有加入你的应用程序,以“保护应用”或应用程序列表中允许在后台运行的名单?在联想应该有像“设置 - >电源 - >后台应用程序管理”。请尝试找到它并将您的应用程序添加到列表中。让我知道你发现了什么。 –

回答

1

这里有几件事情:

请删除“alarmRunning”代码在您设定的报警。只要一直这样做。如果闹钟已经设置,再次设置将取消旧闹钟并设置新闹钟。这不是问题。

你不能依靠PendingIntent的存在,以确定报警是否已在报警管理器中设置。这很可能是您在查杀并重新启动应用程序后没有发出警报的原因。

而且,你不能可靠地使用setRepeating()安排报警每20秒。 Android具有严格的电源管理规则,在大多数设备上不会可靠地触发少于2分钟的重复警报。根据电源管理设置,电池电量,设备的繁忙程度,是否正在睡眠等,您可能会在2或3或5分钟而不是20秒后看到此闹钟。

如果您确实想要某物每20秒运行一次,然后您应设置一个警报,并在警报关闭时处理该警报,并将现在的下一个警报设置为20秒。

+1

谢谢大卫,你的建议对我真的很有用。非常感谢。 –

+1

我很高兴能够提供帮助。 –