【问题标题】:Service not running when app is closed应用程序关闭时服务未运行
【发布时间】:2017-06-03 10:48:22
【问题描述】:

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

在我的活动中,我声明了以下内容来启动警报服务

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();

我的 OnAlarmManager 代码如下所示

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 上运行的设备中触发通知。 但是在 Marshmallow 上运行的设备中,当应用关闭时,服务类永远不会触发

我在这里缺少什么吗?

【问题讨论】:

  • 你可能想先检查这个问题stackoverflow.com/questions/34378707/…
  • 没有帮助。当应用程序运行或在后台运行时触发通知。但是一旦我将应用程序滑开或关闭它,与之相关的所有内容(接收器,服务)都会被破坏

标签: android android-service android-alarms


【解决方案1】:

你能试试前台服务吗? http://www.vogella.com/tutorials/AndroidServices/article.html

从你所说的看来,服务似乎在主线程上运行,并与应用程序一起被杀死。

【讨论】:

    【解决方案2】:

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

    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);
    }
    

    您的服务将如下所示:

    public class OnAlarmManager extends IntentService {
        public static final String ACTION_TRIGGER_ALARM = "com.example.myapplication.ACTION_TRIGGER_ALARM";
    
        private static final String TAG = "OnAlarmReceiver";
    
        private NotificationManager nm;
    
        public OnAlarmManager() {
             super("OnAlarmManager");
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
            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);
            Log.i(TAG, "Alarm has begun");
        }
    }
    

    并在您的 AndroidManifest.xml 中添加操作信息:

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

    【讨论】:

    • 在使用 IntentService 时,它​​要求我在服务类中添加一个构造函数。当我这样做时,添加了以下构造函数类 public OnAlarmManager(String name) { super(name);添加这部分在清单声明中给我一个错误,告诉我 Onalarmmanager 中没有默认构造函数。应用程序也会崩溃
    • 哦,对不起,我在示例代码中遗漏了它。检查更新的答案。
    • 添加了它,就像我提到的问题一样,通知是在后台运行时触发的,但是一旦我关闭应用程序,一切都会被破坏
    • 警报应该被触发,在前台或后台不应该影响它的工作。这很奇怪。
    • 你在哪个设备上测试它?
    【解决方案3】:

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

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

    【讨论】:

    • 已经完成了。如果不这样做,它甚至不会在运行 JellyBean 的设备上运行
    • 它似乎已经在 J​​ellyBean 设备上运行,因此服务声明必须明显存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多