Android服务自动停止

3
我正在开发一个带有闹钟功能的应用程序。为此,我正在使用服务来检查设备当前时间与数据库中的时间是否匹配。
我的问题是,如果从后台删除应用程序或设备重新启动,则该服务将停止。我已经使用了START_STICKY使其在后台运行,并使用广播接收器在重新启动时启动它。
主要问题是,我编写的所有内容都在MOTO G设备上运行良好。无论重启还是从后台清除,该服务都可以正常运行。但是,在小米手机和华为荣耀手机上,一旦从后台清除或重新启动,服务就会停止。
服务代码:
public class RemindService extends Service {

final long delayMillis=500;
Handler h=null;
Runnable r;
SharedPreferences sp;

PendingIntent pendingIntent;

private static final int NOTIFY_ME_ID=1337;
@Override
  public void onCreate() {

    h=new Handler(Looper.getMainLooper());
}


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

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



    r = new Runnable() {

           public void run() {


          //SOME OF MY IF-ELSE CONDITIONS


            Intent myIntent = new Intent(RemindService.this, ReminderPopUp.class);
            int randomPIN = (int)(Math.random()*9000)+1000;
            pendingIntent = PendingIntent.getActivity(RemindService.this, randomPIN, myIntent,PendingIntent.FLAG_ONE_SHOT);
            AlarmManager alarmManager = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC|AlarmManager.RTC_WAKEUP, System.currentTimeMillis() , pendingIntent);



            h.postDelayed(this, delayMillis);

        }
      };

    h.post(r);
    return Service.START_STICKY;

}
@Override
  public void onDestroy() {
    h.removeCallbacks(r);

}

}

我的 Manifest 声明:

<service
        android:name="test.aguai.medieazy.RemindService"
        android:enabled="true" />

    <intent-filter>
        <action android:name="test.aguai.medieazy.START_SERVICE" />
    </intent-filter>

还有其他人遇到过这个问题吗?我认为这是修改后的操作系统的问题,但无论如何,我的应用程序都不能正常工作。请帮忙。


你尝试过AlarmManager吗? - antonio081014
3个回答

2
相较于不断地轮询设备数据库,我会使用AlarmManager服务,就像我在这个回答中所描述的那样:Android Polling from a Server periodically。设置闹钟在第一个预定时间触发。当它触发时,设置下一个时间,以此类推。没有必要一次性设置所有闹钟,因为每次只有一个可以被触发。当闹钟触发时,您可以启动一个服务来执行任何需要的任务(包括设置下一个闹钟)。

但我无法确定下一次响铃的时间,下一个闹钟时间会是什么,因为它是在固定时间段后进行检查的。 - Prakhar
如果您不知道下一个闹钟将在何时响起,那么您在服务中检查什么以了解您需要“触发”闹钟事件呢?在某个时刻,您必须设置要触发闹钟的时间。这就是您设置AlarmManager的地方。 - Kuffs
好的,我明白了。但问题是我的服务会停止。那么该如何设置闹钟管理器呢? - Prakhar
好的,那我该怎么做呢?我想在我的服务中也只使用了闹钟管理器。如果我从后台清除它,它还能工作吗? - Prakhar
文档包含了你所需要的一切。 - Kuffs
显示剩余2条评论

0

试试这样做,它会起作用的

// for Every 6 minutes exact repeating service
                    Intent myIntent2 = new Intent(sign_in.this,MyAlarmService.class);
                    pendingintent3 = PendingIntent.getService(sign_in.this, 2,myIntent2, 2);
                    AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);
                    Calendar calendar2 = Calendar.getInstance();
                    calendar2.setTimeInMillis(System.currentTimeMillis());
                    calendar2.add(Calendar.SECOND, 30);

                    alarmManager2.set(AlarmManager.RTC_WAKEUP,calendar2.getTimeInMillis(), pendingintent3);

                    alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP,calendar2.getTimeInMillis(), 360 * 1000,pendingintent3);

清单权限

 <!-- Web data Sync Service -->
        <service android:name="com.example.MyAlarmService" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </service>

但我不想重复,因为我正在处理警报。如果警报响了一次,然后每隔5分钟再响一次,那将非常令人烦恼。 - Prakhar

0
如果您希望服务一直运行直到明确停止,请考虑调用startService()来启动服务。这允许服务无限期地运行,并且还允许客户端通过调用bindService()绑定到服务。
请记住,您必须显式停止服务,通过调用stopSelf()stopService()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接