run android application at a time

mohammad

I am new in android.

I use bellow code to run application at a special time. but some times when mobile phone is locked and screen is off , service stops and does not work.

public class TimeService extends Service {
IBinder mBinder;
Timer t;
public IBinder onBind(Intent intent) {
    return mBinder;
}
public void onDestroy(){
    t.cancel();
}
public void onTaskRemoved(Intent rootIntent){
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    restartServiceIntent.setPackage(getPackageName());
    PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.A  LARM_SERVICE);
    alarmService.set(
            AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + 1000,
            restartServicePendingIntent);
    super.onTaskRemoved(rootIntent);
}
public int onStartCommand(Intent intent, int flags, int startId) {
    final DbHelper db=new DbHelper(this);
    int time[]=db.getAlarmTime();
    Calendar c = Calendar.getInstance();
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute=c.get(Calendar.MINUTE);
        int rt=(time[0]*3600+time[1]*60)-(hour*3600+minute*60)*1000;
        t=new Timer();
        t.schedule(
                new TimerTask() {
                    public void run() {
                        //codes...
                        stopService(i);
                        startService(i);
                    }
                },rt
        );
    return START_STICKY;
}
}
Hicham

When the device goes to sleep state, your code will not be called, I suggest that you use AlarmManager to handle your code, instead of your service. You can then put your code inside a Receiver's onReceive method, and if you need you can acquire a wakelock until your code is executed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Run time error when run the android application

From Dev

zxing run time exception into android application

From Dev

zxing run time exception into android application

From Dev

Run Android application in multiple devices at the same time (Xamarin)

From Dev

How to get the "last" date or time that the user has run the application in android?

From Dev

android - Run time error

From Dev

Android Application's onCreate called mutiple times, How to guarantee only run initialize res one time

From Dev

Android Studio - Run application with coverage

From Dev

Intellij "Run as Android application" missing

From Dev

How to run Android application on Windows?

From Dev

Decide an application to be of console/windows subsystem at run time

From Dev

How to run 3 dependent application at same time

From Dev

How to run real-time application in Linux?

From Dev

Run more than one application in the same time

From Dev

How to run a Java console application all the time?

From Dev

Real-time Android application

From Dev

Android:Android Service forces application to run slow

From Dev

Android:Android Service forces application to run slow

From Dev

What is the difference between "Run As Android Application" and "Debug As Android Application"?

From Dev

What is the difference between "Run As Android Application" and "Debug As Android Application"?

From Dev

"Run as Android Application" doesn't work

From Dev

Run libgdx application on Android with unlimited FPS?

From Dev

Eclipse libGDX can't run as Android Application

From Dev

Can't run application in android studio

From Dev

How to run Java jdt AST in Android Application

From Dev

Run apk from inside android application

From Dev

Android application crash when run after installation

From Dev

How to run integration tests for a mavenised Android application?

From Dev

Dx Warning when run my android application

Related Related

  1. 1

    Run time error when run the android application

  2. 2

    zxing run time exception into android application

  3. 3

    zxing run time exception into android application

  4. 4

    Run Android application in multiple devices at the same time (Xamarin)

  5. 5

    How to get the "last" date or time that the user has run the application in android?

  6. 6

    android - Run time error

  7. 7

    Android Application's onCreate called mutiple times, How to guarantee only run initialize res one time

  8. 8

    Android Studio - Run application with coverage

  9. 9

    Intellij "Run as Android application" missing

  10. 10

    How to run Android application on Windows?

  11. 11

    Decide an application to be of console/windows subsystem at run time

  12. 12

    How to run 3 dependent application at same time

  13. 13

    How to run real-time application in Linux?

  14. 14

    Run more than one application in the same time

  15. 15

    How to run a Java console application all the time?

  16. 16

    Real-time Android application

  17. 17

    Android:Android Service forces application to run slow

  18. 18

    Android:Android Service forces application to run slow

  19. 19

    What is the difference between "Run As Android Application" and "Debug As Android Application"?

  20. 20

    What is the difference between "Run As Android Application" and "Debug As Android Application"?

  21. 21

    "Run as Android Application" doesn't work

  22. 22

    Run libgdx application on Android with unlimited FPS?

  23. 23

    Eclipse libGDX can't run as Android Application

  24. 24

    Can't run application in android studio

  25. 25

    How to run Java jdt AST in Android Application

  26. 26

    Run apk from inside android application

  27. 27

    Android application crash when run after installation

  28. 28

    How to run integration tests for a mavenised Android application?

  29. 29

    Dx Warning when run my android application

HotTag

Archive