Skip to content

Instantly share code, notes, and snippets.

View Suchiq's full-sized avatar

Suchi Bansal Suchiq

View GitHub Profile
package com.example.featureflaglibrary
import android.content.Context
import com.example.featureflaglibrary.FeatureFlagManager
class FeatureFlagProvider {
lateinit var flagManager: FeatureFlagManager
fun init(applicationContext: Context) {
flagManager = FeatureFlagManager()
package com.example.featureflaglibrary
import android.content.Context
import android.content.SharedPreferences
class FeatureFlagManager : FeatureFlag {
lateinit var sharedPref: SharedPreferences
companion object {
const val feature = "feature"
interface FeatureFlag {
fun setKeyValue(key: String, value: Boolean)
fun getValue(key: String): Boolean
}
boolean permissionAccessCoarseLocationApproved =
ActivityCompat.checkSelfPermission(this,
permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED;
if (permissionAccessCoarseLocationApproved) {
// App has permission to access location in the foreground. Start your
// foreground service that has a foreground service type of "location".
} else {
// Make a request for foreground-only location access.
<service
android:name="MyNavigationService"
android:foregroundServiceType="location" ... >
...
</service>
WorkManager.getInstance()
.beginWith(workRequest)
.then(workRequest1)
.then(workRequest2)
.enqueue();
final PeriodicWorkRequest periodicWorkRequest
= new PeriodicWorkRequest.Builder(MyWorker.class, 10, TimeUnit.HOURS)
.build();
WorkManager.getInstance().cancelWorkById(workRequest.getId());
//creating constraints
Constraints constraints = new Constraints.Builder()
.setRequiresCharging(true) // you can add as many constraints as you want
.build();
final OneTimeWorkRequest workRequest =
new OneTimeWorkRequest.Builder(MyWorker.class)
.setInputData(data)
.setConstraints(constraints)
.build();
final TextView textView = findViewById(R.id.textViewStatus);
WorkManager.getInstance().getWorkInfoByIdLiveData(workRequest.getId())
.observe(this, new Observer<WorkInfo>() {
@Override
public void onChanged(@Nullable WorkInfo workInfo) {
//receiving back the data
if(workInfo != null && workInfo.getState().isFinished())
textView.append(workInfo.getOutputData().getString(MyWorker.TASK_DESC) + "\n");
textView.append(workInfo.getState().name() + "\n");