Android 为什么服务在一两天后就停止了?

Android 为什么服务在一两天后就停止了?,android,Android,我刚刚使用了一个报警管理器来启动一个广播接收器,该广播接收器调用一个服务。整个过程在固定的时间间隔后被一次又一次地调用,正如我在报警服务中已经设置的那样。但我仍然面临着一个问题,我的服务在一些android设备上运行了一两天后就失效了。下面是我的代码 这就是我呼叫广播接收器的地方 Intent myIntent = new Intent(NotificationFirstActivity.this, MyReceiver.class); pendi

我刚刚使用了一个报警管理器来启动一个广播接收器,该广播接收器调用一个服务。整个过程在固定的时间间隔后被一次又一次地调用,正如我在报警服务中已经设置的那样。但我仍然面临着一个问题,我的服务在一些android设备上运行了一两天后就失效了。下面是我的代码

这就是我呼叫广播接收器的地方

Intent myIntent = new Intent(NotificationFirstActivity.this,
                MyReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(
                NotificationFirstActivity.this, 0, myIntent, 0);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(),
                pendingIntent);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(), 1 * 60 * 1000, pendingIntent);
这是我的广播接收器,如下所示

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service1 = new Intent(context, MyAlarmService.class);
        context.startService(service1);
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent pushIntent = new Intent(context, MyAlarmService.class);
            context.startService(pushIntent);
        }

    }

}
下面是我的清单部分

<service android:name="MyAlarmService"
                 android:enabled="true" />

        <receiver android:name="MyReceiver"/>


         <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="com.phoenixmanage" />
            </intent-filter>
        </receiver>
         <service android:name="com.phoenixmanage.GCMIntentService" />

可能会发生以下情况。 1) 如果设备关闭并启动,则您将无法获得报警maanger广播接收器。实施OnBootReceiver,它将接收OnBoot completed,在那里您可以启动未触发的挂起报警。 参考:


2)

这是我在我的一个应用程序中使用的,工作非常完美。我已经用它在服务器上张贴每15分钟的数据

public class BackgroundService extends Service {

        public static final String ACTION_PING = "com.example.ACTION_PING";
        public static final String ACTION_CONNECT = "com.example.ACTION_CONNECT";
        public static final String ACTION_SHUT_DOWN ="com.example.ACTION_SHUT_DOWN";

        private final static String TAG = "BackgroundService";
        static Context context;
        private static volatile PowerManager.WakeLock lockStatic = null;

        long INTERVAL =AlarmManager.INTERVAL_FIFTEEN_MINUTES;

    public static Intent startIntent(Context context) {
    Intent i = new Intent(context, BackgroundService.class);
            i.setAction(ACTION_CONNECT);
            return i;
    }

        public static Intent pingIntent(Context context) {
            Intent i = new Intent(context, BackgroundService.class);
            i.setAction(ACTION_PING);
            return i;
        }

        public static Intent closeIntent(Context context) {
            Intent i = new Intent(context, BackgroundService.class);
            i.setAction(ACTION_SHUT_DOWN);
            return i;
        }

        synchronized private static PowerManager.WakeLock getLock(Context context) {
            if (lockStatic == null) {
                PowerManager mgr = (PowerManager) context.getApplicationContext().getSystemService(Context.POWER_SERVICE);

                lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
                lockStatic.setReferenceCounted(true);
            }

            return (lockStatic);
        }



        @Override
        public void onCreate() {
            super.onCreate();
            Log.d(TAG, "onCreate");
    context = this;
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.i(TAG, "onDestroy");
        }

        @Override
        public void onStart(Intent intent, int startId) {
            super.onStart(intent, startId);
            Log.i(TAG, "onStart");

        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // Log.i(TAG, "onStartCommand");

            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock lock = getLock(this.getApplicationContext());

            if (!lock.isHeld()) {
                lock.acquire();
            }

            if (intent != null) {
                if (ACTION_SHUT_DOWN.equals(intent.getAction())) {
                    stopSelf();
                    return super.onStartCommand(intent, flags, startId);
                }
            }

            if (intent == null || (intent.getAction() != null && !intent.getAction().equals(ACTION_SHUT_DOWN))) {
                AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                PendingIntent operation = PendingIntent.getService(this, 0,
                BackgroundService.pingIntent(this),
                PendingIntent.FLAG_NO_CREATE);
                if (operation == null) {
                PendingIntent operation = PendingIntent.getService(this, 0, BackgroundService.pingIntent(this), PendingIntent.FLAG_UPDATE_CURRENT);
                am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pm.isScreenOn() ? SCREEN_ON_INTERVAL : SCREEN_OFF_INTERVAL, operation);
                }
            }

            //fetch parsms and make your web service call here

            return START_STICKY;
        } 
使用此选项启动服务

startService(BackgroundService.startIntent(context));
 AlarmManager am = (AlarmManager)
                context.getSystemService(Context.ALARM_SERVICE);
                 PendingIntent operation = PendingIntent.getService(context, 0,
                 BackgroundService.pingIntent(context),
                PendingIntent.FLAG_NO_CREATE);
                 if (operation != null) {
                 am.cancel(operation);
                 operation.cancel();
                }
                context.startService(BackgroundService.closeIntent(context));
使用此选项可终止服务

startService(BackgroundService.startIntent(context));
 AlarmManager am = (AlarmManager)
                context.getSystemService(Context.ALARM_SERVICE);
                 PendingIntent operation = PendingIntent.getService(context, 0,
                 BackgroundService.pingIntent(context),
                PendingIntent.FLAG_NO_CREATE);
                 if (operation != null) {
                 am.cancel(operation);
                 operation.cancel();
                }
                context.startService(BackgroundService.closeIntent(context));

注意:如果您的设备重新启动,您的警报将被取消,因此,如果你想重新创建它,你应该创建一个BroadcastReceiver,监听android.intent.action.BOOT_完成的操作,并允许你调用上面的代码来设置重复警报。

重新启动时检查,即使重新启动“我的服务”后,你也会得到接收器或便笺的日志,主要问题是,在两到三天的服务后,每个设备都会自动销毁。每个设备都面临问题!!没有,因为我在我的htc desire中有应用程序,它工作正常,但在三星有问题。在另一个型号上再次测试它。那么它的硬件问题!!硬件科的职位