0

There is a lot of Answers like this answer https://stackoverflow.com/a/19856367/7742857, But still i didn't get the solution i wanted.

The is the Manifest file

 <uses-permission
        android:name="android.permission.PACKAGE_USAGE_STATS"
        tools:ignore="ProtectedPermissions" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<service android:name=".MyService" />
        <receiver
            android:name=".RestartService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.DEFAULT"/>

            </intent-filter>
        </receiver>

This is Service class, I'm not using onStartCommand();

     @Override
    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.ALARM_SERVICE);
        assert alarmService != null;
        alarmService.set(
                AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + 1000,
                restartServicePendingIntent);
        super.onTaskRemoved(rootIntent);
        Log.e("Service_Auto_Restart", "ON");
    }

This class is inside Service class

   class TimeDisplayTimerTask extends TimerTask {


        @Override
        public void run() {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    Timer();
                    }
            });
        }

    }

and this is is BroadCast Class

   public class RestartService extends BroadcastReceiver {
    MyService myService;
    MyService.TimeDisplayTimerTask timeDisplayTimerTask;
    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            context.startService(new Intent(context,MyService.class));
            myService.getPackagesList("package_names");
            myService.getTimer("SELECTED_TIME");
            myService.Timer();
            timeDisplayTimerTask.run();
            Toast.makeText(context,"Restarted",Toast.LENGTH_LONG).show();
        }

    }
}

I would be grateful for any help you are able to provide.

ismail
  • 167
  • 1
  • 8

2 Answers2

0

Your BroadcastReceiver class is broken. You can never have an instance of a Service. And in your own code its never initialized, it will just cause a NullPointerException. So you may well be getting one and crashing.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • what i have to do now, Can you help me ? i know my code is not correct, but i didn't get any other solution, even Toast messge I'm not getting. – ismail Sep 16 '19 at 12:52
  • 1
    Because you're crashing before you get to the Toast. Remove all the references to myService, they won't work. I can't even tell you how to fix it because I can't tell what you're trying to do. But I can tell you that trying to have an infinitely running service via restart like you're trying won't work on many versions of Android. That's against everything Google is trying to do to save battery life, and you need to find another way to do things. – Gabe Sechan Sep 16 '19 at 13:08
  • Now i can get the Toast messge after i removed the Service Class. I'm trying to build AppLock which has Timer instead of password, and i want to keep running the service class which has CountDownTimer, So now when the phone i switch OFFand again ON the service is stopping. – ismail Sep 16 '19 at 13:55
  • But i want to Restart the Service again – ismail Sep 16 '19 at 14:04
  • @ismail that's something you can't reliably do in Android. Purposely so. Find another way to do what you're actually trying to do that doesn't require a permanent service. – Gabe Sechan Sep 16 '19 at 17:56
0

You don't need a service to do this, you can actually do it in broadcast. I'm using kotlin, i just call another function to do the work, but it's all inside my broadcast.

Omkar
  • 3,040
  • 1
  • 22
  • 42
Jhonatan Sabadi
  • 838
  • 7
  • 14