gpt4 book ai didi

java - 如何在 Android 中使用 Intent 向服务发送信息

转载 作者:行者123 更新时间:2023-12-02 04:24:11 25 4
gpt4 key购买 nike

我创建了一项更新 GPS 的服务坐标并将其发送至FireBase Realtime Database 。当用户访问主页时该服务启动。我希望能够从 Activity 发送字符串到服务类。问题是我无法调用 getIntent() loginToFirebase() 内的方法功能。

这是我尝试过的代码:

服务类别:

 public class TrackerService extends Service {
private static final String TAG = TrackerService.class.getSimpleName();


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

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

buildNotification();

loginToFirebase();

}

private void buildNotification() {
String stop = "stop";

registerReceiver(stopReceiver, new IntentFilter(stop));

PendingIntent broadcastIntent = PendingIntent.getBroadcast(

this, 0, new Intent(stop), PendingIntent.FLAG_UPDATE_CURRENT);

// Create the persistent notification

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setContentText("Suivi de la position ...")
.setOngoing(true)
.setContentIntent(broadcastIntent)
.setSmallIcon(R.drawable.alert);
startForeground(1, builder.build());
}

protected BroadcastReceiver stopReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "received stop broadcast");
// Stop the service when the notification is tapped
unregisterReceiver(stopReceiver);
stopSelf();
}
};

private void loginToFirebase() {

String password = "myPassword";
FirebaseAuth.getInstance().signInWithEmailAndPassword(
email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>(){
@Override
public void onComplete(Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "firebase auth success");
requestLocationUpdates();
} else {
Log.d(TAG, "firebase auth failed");
}
}
});
}

private void requestLocationUpdates() {
LocationRequest request = new LocationRequest();
request.setInterval(30000);
request.setFastestInterval(30000);
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
FusedLocationProviderClient client = LocationServices.getFusedLocationProviderClient(this);
final String path = "locations" + "/" + 123;
int permission = ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION);
if (permission == PackageManager.PERMISSION_GRANTED) {
client.requestLocationUpdates(request, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
try {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(path);
Location location = locationResult.getLastLocation();
if (location != null) {
Log.d(TAG, "location update " + location);
ref.setValue(location);
}
}catch (Exception e){

}
}
}, null);
}
}

}

我调用服务的方法(在 Activity 中):

  private void startTrackerService() {
Intent goService= new Intent(AccueilEtudiant.this,TrackerService.class);
goService.putExtra("email",getIntent().getStringExtra("email"));
startService(goService);

}

有什么建议吗?

最佳答案

我建议你使用持久性。在您的服务中使用从 sharedPreferences 获取的数据,并在您的 Activity 中不断更新 sharedPreferences

GPS 信息已更新 => 保留到存储 => 在服务中使用保留的 GPS 信息

服务必须在发送数据之前从所选存储中读取数据。

关于java - 如何在 Android 中使用 Intent 向服务发送信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56615357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com