调用API和提取数据的前景

问题描述

我正在创建一个应用程序,以将数据从服务器拉到应用程序的本地数据库中。即使锁定设备屏幕,该应用程序也应提取数据。我们仅针对Android 10。我发现最好的选择是使用前台服务。设备RAM和电池使用量对我们来说不是问题。

下面的类创建一个前台服务。我想知道我该在哪里编写代码调用API,以从服务器获取最新数据。

public class MyService extends Service {

    private static final int ID_SERVICE = 101;

    @Override
    public int onStartCommand(Intent intent,int flags,int startId) {
        super.onStartCommand(intent,flags,startId);
        return START_STICKY;
    }

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

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

        notificationmanager notificationmanager = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);
        String channelId = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? createNotificationChannel(notificationmanager) : "";
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,channelId);
        Notification notification = notificationBuilder.setongoing(true)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Foreground Service")
                .setContentText("Always running...")
                .build();

        startForeground(ID_SERVICE,notification);
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private String createNotificationChannel(notificationmanager notificationmanager){
        String channelId = "my_service_channelid";
        String channelName = "My Foreground Service";
        NotificationChannel channel = new NotificationChannel(channelId,channelName,notificationmanager.IMPORTANCE_HIGH);
        channel.setImportance(notificationmanager.IMPORTANCE_NONE);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        notificationmanager.createNotificationChannel(channel);
        return channelId;
    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...