简体   繁体   中英

How to run java In background

How to run this in background , I mean even I move to other app or go to home screen of my android or close the screen , the button will still clicking itself please help me

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
button1.performClick();
}
}, 5000);

Things to know

I will try to elaborate as much as I can in a layman terms so that you have a better grasp the Idea of Threads and async tasks

new Handler().postDelayed(new Runnable() {
   @Override
   public void run() {
     //business logic
   }
}, 5000);
  1. is an Blocking method, which runs on the UI thread (I am supposing you are new to programming/android)[please read about Threads to understand what I am saying in deapth],
  2. which means, in short, your application is executing some logic on the thread ("A worker" which is responsible for the rendering the UI on-screen),
  3. By using Threads you can achieve efficiency in your application by dividing multiple tasks to multiple workers " Threads " but you can't run your application in the background.

How to make your application work in the background?

Google introduced some background limitations in Android Oreo. so to keep your application alive you need

  1. foreground service by showing an ongoing notification.

1. The way you should implement service is like

public class YourService extends Service {

private static final int NOTIF_ID = 1;
private static final String NOTIF_CHANNEL_ID = "Channel_Id";

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

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

    // do your jobs here

    startForeground();
    
    return super.onStartCommand(intent, flags, startId);
}

private void startForeground() {
    Intent notificationIntent = new Intent(this, MainActivity.class);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    startForeground(NOTIF_ID, new NotificationCompat.Builder(this, 
            NOTIF_CHANNEL_ID) // don't forget create a notification channel first
            .setOngoing(true)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(getString(R.string.app_name))
            .setContentText("Service is running background")
            .setContentIntent(pendingIntent)
            .build());         
   }
}

2. Also you need to start the service

public class App extends Application {

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

        startService(new Intent(this, YourService.class));
    }
}

3. Add your service in the "application" tag of your AndroidManifest.xml

<service android:name=".YourService"/>

4. And also this permission request in the "manifest" tag (if API level 28 or higher)

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

In this way, you can keep your service in the background. I suggest you read articles and see GitHub repositories, and also practice practice practice a lot to be good at Android :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM