I found a more efficient and reliable solution, I registered a notification manager that works every 5 seconds and calls the method in my main action!
MainActivity.java
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // ENABLE MyReceiver SERVICE ComponentName receiver = new ComponentName(MainActivity.this, NotifyService.class); PackageManager pm = this.getPackageManager(); pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); // Toast.makeText(this, "Enabled broadcast receiver", // Toast.LENGTH_SHORT) // .show(); // --// Intent intent = new Intent(this, NotifyService.class); PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager am = (AlarmManager) this .getSystemService(Context.ALARM_SERVICE); long recurring = (1 * 1000 * 5); // in milliseconds am.setRepeating(AlarmManager.RTC, Calendar.getInstance() .getTimeInMillis(), recurring, sender); } public static void mehtodName(Context context) { KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); if( myKM.inKeyguardRestrictedInputMode() ) { // it is locked task(context); } else { //it is not locked } } private static void task(Context context) { // Process Killer and display all package names in toast ActivityManager actvityManager = (ActivityManager) context .getApplicationContext().getSystemService( context.getApplicationContext().ACTIVITY_SERVICE); List<RunningAppProcessInfo> procInfos = actvityManager .getRunningAppProcesses(); for (int pnum = 0; pnum < procInfos.size(); pnum++) { actvityManager .killBackgroundProcesses(procInfos.get(pnum).processName); } } }
NotifyService.java
public class NotifyService extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { MainActivity.mehtodName(context);
My manifest
<receiver android:name="com.example.notifypro.NotifyService" > </receiver>
source share