[英]Android - How to send an ArrayList without using Intent?
[英]How to send information to a service using Intent in Android
我創建了一個更新GPS
坐標並將它們發送到FireBase Realtime Database
。 該服務在用戶訪問主頁時啟動。 我希望能夠將一個String從一個Activity
發送到服務類。問題是我不能調用loginToFirebase()
函數內的getIntent()
方法。
這是我試過的代碼:
服務類:
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);
}
}
}
我正在調用服務的方法(在活動中):
private void startTrackerService() {
Intent goService= new Intent(AccueilEtudiant.this,TrackerService.class);
goService.putExtra("email",getIntent().getStringExtra("email"));
startService(goService);
}
有什么建議?
我建議你使用持久性。 在您的服務中使用您從sharedPreferences
獲得的數據,並在您的活動中,不斷更新sharedPreferences
gps info got updated => persist to storage =>在服務中使用持久的gps信息
服務必須在發送數據之前從所選存儲中讀取數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.