当应用程序完全关闭时,我的服务无法运行

时间:2017-06-02 07:52:18

标签: android notifications android-service alarmmanager android-background

我知道有很多关于这个问题的问题,我几乎看到了所有问题,但我无法解决我的问题。我有一个服务,使用本地广播管理器调用主要活动,我从这个服务获得一些价值,然后我把它们放入字符串并发送到需要使用它们的其他服务。其中一个服务调用本地广播,但其中两个在按钮监听器中,我使用警报管理器每1分钟重复运行它们,我也在第一个服务中使用satrtforeground()调用主要活动,但奇怪的是在设备中的某些设备屏幕下车或用户从后台(从堆栈)清理应用程序服务不起作用。我希望你能帮助我,谢谢

public class ServiceURL extends Service {


public static final String Service = ServiceURL.class.getName() + "LocationBroadcast";




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

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


    Notification note = new Notification( 0, null, System.currentTimeMillis() );
    note.flags |= Notification.FLAG_NO_CLEAR;
    startForeground( 42, note );


   // some codee

        Intent i = new Intent(Service);
        i.putExtra("Sobh",prayerTimes.get(0).toString());
        i.putExtra("Sunrise",prayerTimes.get(1).toString());
        i.putExtra("Zohr",prayerTimes.get(2).toString());
        i.putExtra("Asr",prayerTimes.get(3).toString());
        i.putExtra("Sunset",prayerTimes.get(4).toString());
        i.putExtra("Maghreb",prayerTimes.get(5).toString());
        i.putExtra("Isha",prayerTimes.get(6).toString());

        sendBroadcastMessage(i);



    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
}
@Override
public void onCreate() {
    super.onCreate();

}

private void sendBroadcastMessage(Intent intent) {

        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}


}

MainActivity.java

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

 // some code 
            LocalBroadcastManager.getInstance(this).registerReceiver(
                    new BroadcastReceiver() {
                        @Override
                        public void onReceive(Context context, Intent intent) {



                             Sobh = intent.getStringExtra("Sobh");
                             Sunrise = intent.getStringExtra("Sunrise");
                             Zohr = intent.getStringExtra("Zohr");
                             Sunset = intent.getStringExtra("Sunset");
                             Maghreb = intent.getStringExtra("Maghreb");
                             Asr = intent.getStringExtra("Asr");
                             Isha = intent.getStringExtra("Isha");

                            Log.d("Sobh in main", Sobh);

                                Intent i = new Intent(getApplicationContext(), ServiceAzan.class);

                                i.putExtra("SOBH", Sobh.substring(0, 5));
                                i.putExtra("ZOHR", Zohr.substring(0, 5));
                                i.putExtra("MAGHREB", Maghreb.substring(0, 5));
                                i.putExtra("ASR", Asr.substring(0, 5));
                                i.putExtra("ISHA", Isha.substring(0, 5));
                                PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 0, i, 0);
                                AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                                alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),60 * 1000, pintent);
                                //   String timestamp = intent.getStringExtra("Date");

                            SobhText.setText(Sobh.substring(0, 5));
                            SunriseText.setText(Sunrise.substring(0, 5));
                            ZohrText.setText(Zohr.substring(0, 5));
                            SunsetText.setText(Sunset.substring(0, 5));
                            MaghrebText.setText(Maghreb.substring(0, 5));
                            AsrText.setText(Asr.substring(0, 5));
                            IshaText.setText(Isha.substring(0, 5));

                            Log.d("sobh text .....", SobhText.getText().toString());


                            Log.d("change value before",beforepick+"");
                            Log.d("change value after",afterpick+"");

                            TimeChanging.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {

                                       // some code 

                                            Intent intentBA = new Intent(getApplicationContext(), ServiceSetTime.class);
                                            Intent sms = new Intent(getApplicationContext(), SmsService.class);

                                            intentBA.putExtra("SOBH", Sobh.substring(0, 5));
                                            intentBA.putExtra("ZOHR", Zohr.substring(0, 5));
                                            intentBA.putExtra("MAGHREB", Maghreb.substring(0, 5));
                                            intentBA.putExtra("ASR", Asr.substring(0, 5));
                                            intentBA.putExtra("ISHA", Isha.substring(0, 5));

                                            PendingIntent pintentBA = PendingIntent.getService(getApplicationContext(), 0, intentBA, 0);
                                            AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                                            alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30 * 1000, pintentBA);

                                            sms.putExtra("SOBH", Sobh.substring(0, 5));
                                            sms.putExtra("ZOHR", Zohr.substring(0, 5));
                                            sms.putExtra("MAGHREB", Maghreb.substring(0, 5));
                                            sms.putExtra("ASR", Asr.substring(0, 5));
                                            sms.putExtra("ISHA", Isha.substring(0, 5));

                                            PendingIntent pintentsms = PendingIntent.getService(getApplicationContext(), 0, sms, 0);
                                            AlarmManager alarmsms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                                            alarmsms.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60 * 1000, pintentsms);

                                           // some code
                                });

                        }
                    }, new IntentFilter(ServiceURL.Service)
            );


                }
            });
        }


}


@Override
protected void onResume() {
    super.onResume();
    startService(new Intent(this, ServiceURL.class));
     }

@Override
protected void onPause() {
    super.onPause();
  //  stopService(new Intent(this, ServiceURL.class));

}

注意:由于编辑代码,这里可能有一些括号不正确,请忽略它们谢谢

2 个答案:

答案 0 :(得分:0)

确保您在退出时没有结束应用程序的过程。使用

finishAffinity(); 

退出应用。

答案 1 :(得分:0)

取决于设备的型号。如果从应用程序堆栈中删除应用程序,则不同的设备会有不同的行为。

某些设备可以停止应用程序进程,并且不会启动任何一个警报管理器,但是其他设备仅关闭活动但进程处于活动状态。

尝试使用https://stackoverflow.com/a/16698105/2930077确定进程是否存在