forked from Yukams/background_locator_fixed
-
Notifications
You must be signed in to change notification settings - Fork 0
Setup
kuloud edited this page Nov 6, 2023
·
3 revisions
Make sure to have the following permissions inside your AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.GET_TASKS" />
- ACCESS_BACKGROUND_LOCATION To get update in background.
- WAKE_LOCK to not sleep while getting the GPS.
- FOREGROUND_SERVICE To let the plugins operate as a service.
- POST_NOTIFICATIONS Android 13(API 级别 33)及更高版本支持用于从应用发送非豁免(包括前台服务 [FGS])通知的运行时权限:POST_NOTIFICATIONS。此更改有助于用户专注于最重要的通知。
- GET_TASKS Cactus判断当前是否为主进程
Add also the following lines to your AndroidManifest.xml:
<service android:name="yukams.app.background_locator_2.IsolateHolderService"
android:permission="android.permission.FOREGROUND_SERVICE"
android:exported="true"
android:foregroundServiceType = "location"/>
<service
android:name="com.gyf.cactus.service.CactusJobService"
android:permission="android.permission.BIND_JOB_SERVICE" />
<service android:name="com.gyf.cactus.service.HideForegroundService" />
<service
android:name="com.gyf.cactus.service.LocalService"
android:enabled="true"
android:exported="true" />
<service
android:name="com.gyf.cactus.service.RemoteService"
android:enabled="true"
android:exported="true"
android:process=":cactusRemoteService" />
<meta-data
android:name="flutterEmbedding"
android:value="2" />
最后,建议在android工程中创建App实例,完成 Cactus 的初始化
class FloveApp : Application() {
override fun onCreate() {
super.onCreate()
val intent = Intent(this, getMainActivityClass(this))
intent.action = Keys.NOTIFICATION_ACTION
val pendingIntent: PendingIntent = PendingIntent.getActivity(
this,
1, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
cactus {
isDebug(true)
setPendingIntent(pendingIntent)
setChannelId(Keys.CHANNEL_ID)
setChannelName(getString(R.string.app_name))
setTitle(getString(R.string.app_name))
setContent(getString(R.string.app_is_running))
}
}
private fun getMainActivityClass(context: Context): Class<*>? {
val packageName = context.packageName
val launchIntent = context.packageManager.getLaunchIntentForPackage(packageName)
val className = launchIntent?.component?.className ?: return null
return try {
Class.forName(className)
} catch (e: ClassNotFoundException) {
e.printStackTrace()
null
}
}
}
Add the following lines to AppDelegate class:
import cactus_locator
func registerPlugins(registry: FlutterPluginRegistry) -> () {
if (!registry.hasPlugin("BackgroundLocatorPlugin")) {
GeneratedPluginRegistrant.register(with: registry)
}
}
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
BackgroundLocatorPlugin.setPluginRegistrantCallback(registerPlugins)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
In app settings enable Background Modes and check Location Updates.
In Info.plist add Key for using location service:
NSLocationAlwaysAndWhenInUseUsageDescription
NSLocationWhenInUseUsageDescription