Android 应用关闭时服务未运行

Android 应用关闭时服务未运行,android,android-service,android-alarms,Android,Android Service,Android Alarms,我正在尝试使用AlarmManager和服务实现报警应用程序 在我的活动中,我声明了以下内容以启动alarmservice Intent intent = new Intent(getApplicationContext(), OnAlarmManager.class); PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 234324243, intent, 0); Al

我正在尝试使用AlarmManager和服务实现报警应用程序

在我的活动中,我声明了以下内容以启动alarmservice

Intent intent = new Intent(getApplicationContext(), OnAlarmManager.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
        234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i * 1000), pendingIntent);
Toast.makeText(getApplicationContext(), "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show();
我的管理员代码如下所示

public class OnAlarmManager extends Service{
    private static final String TAG = "OnAlarmReceiver";

    private NotificationManager nm;

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent mainActivityIntent = new Intent(this, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, mainActivityIntent, 0);

        //setting up notification
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("New Notification")
                .setContentText("This is a successfull app on service")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pIntent)
                .build();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        nm.notify(0, notification);
        Toast.makeText(getApplicationContext(), "Alarm has begun", Toast.LENGTH_LONG).show();
        Log.i(TAG, "Alarm has begun");
         return START_STICKY;
    }
}
现在,当我尝试运行此程序时,即使在设置警报后关闭应用程序,在给定时间在JellyBean上运行的设备中也会触发通知。 但在棉花糖上运行的设备中,当应用程序关闭时,永远不会触发服务类


这里缺少的是什么吗?

只需将Manifest.xml文件中的服务注册为

<service android:name=".YourServiceClassName" />

为了方便起见,请尝试使用
AlarmManager.setExact
IntentService
。您的代码将如下所示:

Intent intent = new Intent(OnAlarmManager.ACTION_TRIGGER_ALARM);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
        234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Toast.makeText(getApplicationContext(), "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show();

if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i * 1000), pendingIntent);
} else {
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i * 1000), pendingIntent);
}
并在AndroidManifest.xml中添加操作信息:

<service
    android:name=".OnAlarmManager"
    android:exported="true">
    <intent-filter>
        <action android:name="com.example.myapplication.ACTION_TRIGGER_ALARM" />
    </intent-filter>
</service>

您能试试前台服务吗


从您所说的来看,该服务似乎是在主线程上运行的,它与应用程序一起被终止。

已经完成了。如果不这样做,它甚至不会在运行JellyBean的设备上运行,因为它似乎已经在JellyBean设备上运行了,所以服务声明显然必须存在。您可能想先检查这个问题,但没有帮助。应用程序正在运行或在后台运行时会触发通知。但一旦我刷走或关闭应用程序,在使用IntentService时,与之相关的所有内容(接收器、服务)都会被破坏,这要求我在服务类中添加一个构造函数。当我这样做时,下面的构造函数类被添加到ArmManager(字符串名){super(name);}添加此部分会在清单声明中给我一个错误,告诉我Onalarmmanager中没有默认构造函数。此外,应用程序崩溃哦,对不起,我在示例代码中错过了它。检查更新的答案。添加它,就像我提到的问题一样,在后台运行时会触发通知,但一旦我关闭应用程序,一切都会被破坏。应触发报警,处于前台或后台不应影响其工作。真奇怪,你在哪台设备上测试?
<service
    android:name=".OnAlarmManager"
    android:exported="true">
    <intent-filter>
        <action android:name="com.example.myapplication.ACTION_TRIGGER_ALARM" />
    </intent-filter>
</service>