如何使用 Thread 调用 getDataFromApi() 函数

How Call the getDataFromApi() function using Thread

我正在为我的移动应用程序获取 google 日历 API 数据。我想不时检查日历的变化。为此,我知道,我必须使用线程。

if (isChecked) {
                    Runnable runnable = new Runnable() {
                        public void run() {
                            long endTime = System.currentTimeMillis() + 1000;

                            while (System.currentTimeMillis() < endTime) {
                                synchronized (this) {
                                    try {
                                        getResultsFromApi();
                                    } catch (Exception e) {}
                                }
                            }
                        }
                    };
                    Thread mythread = new Thread(runnable);
                    mythread.start();
                    Toast.makeText(AlertActivity.this, "Calendar Settings Enabled",
                            Toast.LENGTH_SHORT).show();
                } else {
                    // The toggle is disabled
                    for(int i=0;i<=broadcastCodeCal;i++)
                        cancel_Alarm(i);
                    Toast.makeText(AlertActivity.this, "Calendar Settings Disabled",
                            Toast.LENGTH_SHORT).show();
                }

我想知道如何通过移动应用程序查看日历详细信息更改时间...请指导我...

创建一个 JobScheduler 如下

 public static void scheduleJob(Context context) {
        ComponentName serviceComponent = new ComponentName(context, TestJobService.class);
        JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent);
        builder.setMinimumLatency(1 * 1000); // wait at least
        builder.setOverrideDeadline(3 * 1000); // maximum delay
        JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
        jobScheduler.schedule(builder.build());
    }

创建以下接收器

public class MyStartServiceReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Util.scheduleJob(context);
    }
}

在 Android 清单中为 BOOT_COMPLETED 事件注册接收器。

<receiver android:name="MyStartServiceReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

创建一个 JobService 并将现有代码添加到 onStartJob

public class TestJobService extends JobService {
    private static final String TAG = "SyncService";

    @Override
    public boolean onStartJob(JobParameters params) {
        Intent service = new Intent(getApplicationContext(), LocalWordService.class);
        getApplicationContext().startService(service);
        Util.scheduleJob(getApplicationContext()); // reschedule the job
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        return true;
    }

}

更多详情请参考:linkhere