Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我需要让我的应用程序在后台工作,即使它关闭_Android_Background - Fatal编程技术网

Android 我需要让我的应用程序在后台工作,即使它关闭

Android 我需要让我的应用程序在后台工作,即使它关闭,android,background,Android,Background,我是android的初学者,我想让我的应用程序在后台循环运行,即使它是关闭的。 有人能帮我吗? 谢谢 您需要创建一个后台服务。请注意,您还需要创建一个在服务运行期间可见的通知: public class BgService extends Service { @Override public int onStartCommand(Intent i, int flags, int startId) { startForeground(C.MAIN_SERVICE_N

我是android的初学者,我想让我的应用程序在后台循环运行,即使它是关闭的。 有人能帮我吗?
谢谢

您需要创建一个后台服务。请注意,您还需要创建一个在服务运行期间可见的通知:

public class BgService extends Service {
    @Override
    public int onStartCommand(Intent i, int flags, int startId) {
        startForeground(C.MAIN_SERVICE_NOTIFICATION_ID, buildNotification(getString(R.string.service_title)));
        return START_NOT_STICKY;
    }

    protected Notification buildNotification(String content) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setTicker(getString(R.string.app_name))
                .setContentTitle(getString(R.string.app_name))
                .setContentText(content)
                .setSmallIcon(R.drawable.notification_icon)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.app_icon))
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(false)
                .setOngoing(true)
                .setContentIntent(pendingIntent);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
            builder.setPriority(Notification.PRIORITY_HIGH);

        Notification notification = builder.build();
        notification.flags |= Notification.FLAG_NO_CLEAR;
        return notification;
    }
}
您可以从应用程序启动此服务:

public class MyApp extends Application {
    @Override
    public void onCreate() {
        startService(new Intent(this, BgService.class));
    }
}

使用服务。。?我想你应该自己搜索一下。。。