0

I know there are many question about this problem and i saw almost all of them, but i can't solve my issue . I have a service which call main activity using local broadcast manager , i get some value from this service and then i put them into string and send into other services that need to use them . one of these services called into local broadcast but two of them in a button listener , i use alarm manager to run them repeatedly each 1 minute, also I use satrtforeground() in first service which call main activity , but strangely in some devices when device screen get off or user clean the app from background ( from stack ) services don't work . I hope you can help me , Thanks

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

}

NOTE: because of editing code here maybe some parenthesis not true , ignore them Thanks

hdiz
  • 1,141
  • 2
  • 13
  • 27

2 Answers2

0

Make sure you are not ending the process of the app while exiting. Use

finishAffinity(); 

To exit the app.

Sarthak Gandhi
  • 2,130
  • 1
  • 16
  • 23
  • no :(( still the same , of course as i said before in some devices not work not all of them , for example in huwaie device – hdiz Jun 02 '17 at 08:08
0

It is depending of model of device. Different devices behave differently if your remove application from stack of application.

Some devices can stop process of application and no one alarm manager will be started, but another devices close only activity but process is alive.

Try use https://stackoverflow.com/a/16698105/2930077 to determine process is alive or not

user2930077
  • 135
  • 2
  • 9
  • so, what can i do for working in all devices ? do you mean it's impossible? – hdiz Jun 02 '17 at 08:16
  • Yes, if an user stops app independently, so the app cannot be run again till the user runs manually. Or you can try to register ACTION_BOOT_COMPLETED or listen notification it can help to run your app again – user2930077 Jun 02 '17 at 08:24
  • when screen off and trackpad activated the same problem occured and it's free of user intervention – hdiz Jun 02 '17 at 08:26
  • Which android version do you use? In marshmallow version device may have aggressive policy including doze mode. Also device can have limited background process. View settings of device, battery optimization. The repeating alarm starting from API 19 are inexact (view doc) and can be performed later. – user2930077 Jun 02 '17 at 08:53