2016-06-23 48 views
1

我做了一個服務,它在位置發生變化之後創建一個意向,並將位置數據放入其中。Android Intent.getStringExtra()在BroadcastReceiver中返回null

intent.putExtra("Latitude", String.valueOf(loc.getLatitude())); //Double 
      intent.putExtra("Longitude", String.valueOf(loc.getLongitude())); //Double 
      intent.putExtra("Provider", loc.getProvider()); //String 
      intent.putExtra("Altitude", loc.getAltitude());//Double 
      intent.putExtra("Accuracy", loc.getAccuracy()); //float 
      intent.putExtra("Speed", loc.getSpeed()); //float 

現在,我有一個BroadcastReceiver每30秒被髮射一次,以便將該信息發佈到服務器。然而,當我試圖讓意圖:

 Intent myIntent = new Intent(context, GPSService.class); //GPSService is the service 
     String lat = (String) myIntent.getExtras().get("Latitude"); 
     String longi = (String) myIntent.getExtras().get("Longitude"); 

我得到了NullPointerException

試圖調用虛擬方法java.lang.Object繼承 android.os.Bundle.get(java.lang中.String)'爲null對象引用

這是爲什麼?我檢查了其他解決方案:有人說是否將正確的Dateformat轉換爲意圖(在這種情況下是String),我曾這樣做過,有人說使用getIntent().getExtras().getString("aKey");我也嘗試過,如上所述,但我仍然從String中獲得null。

將它傳遞給broadcastreceiver時必須有一些問題,因爲我在做了intent.putExtra()之後記錄了這些值,而且它們絕對不是null。

編輯:

GPSService:

package com.dandddeveloper.alarmisto; 

import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.util.Log; 
import android.widget.Toast; 

public class GPSService extends Service 
{ 
    public static final String BROADCAST_ACTION = "Hello World"; 
    private static final int TWO_MINUTES = 1000 * 60 * 2; 
    public LocationManager locationManager; 
    public MyLocationListener listener; 
    public Location previousBestLocation = null; 

    Intent intent; 
    int counter = 0; 

    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 
     intent = new Intent(BROADCAST_ACTION); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) 
    { 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     listener = new MyLocationListener(); 
     //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener); 
    } 

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

    protected boolean isBetterLocation(Location location, Location currentBestLocation) { 
     if (currentBestLocation == null) { 
      // A new location is always better than no location 
      return true; 
     } 

     // Check whether the new location fix is newer or older 
     long timeDelta = location.getTime() - currentBestLocation.getTime(); 
     boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; 
     boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; 
     boolean isNewer = timeDelta > 0; 

     // If it's been more than two minutes since the current location, use the new location 
     // because the user has likely moved 
     if (isSignificantlyNewer) { 
      return true; 
      // If the new location is more than two minutes older, it must be worse 
     } else if (isSignificantlyOlder) { 
      return false; 
     } 

     // Check whether the new location fix is more or less accurate 
     int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); 
     boolean isLessAccurate = accuracyDelta > 0; 
     boolean isMoreAccurate = accuracyDelta < 0; 
     boolean isSignificantlyLessAccurate = accuracyDelta > 200; 

     // Check if the old and new location are from the same provider 
     boolean isFromSameProvider = isSameProvider(location.getProvider(), 
       currentBestLocation.getProvider()); 

     // Determine location quality using a combination of timeliness and accuracy 
     if (isMoreAccurate) { 
      return true; 
     } else if (isNewer && !isLessAccurate) { 
      return true; 
     } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { 
      return true; 
     } 
     return false; 
    } 



    /** Checks whether two providers are the same */ 
    private boolean isSameProvider(String provider1, String provider2) { 
     if (provider1 == null) { 
      return provider2 == null; 
     } 
     return provider1.equals(provider2); 
    } 



    @Override 
    public void onDestroy() { 
     // handler.removeCallbacks(sendUpdatesToUI); 
     super.onDestroy(); 
     Log.v("STOP_SERVICE", "DONE"); 
     locationManager.removeUpdates(listener); 
    } 

    public static Thread performOnBackgroundThread(final Runnable runnable) { 
     final Thread t = new Thread() { 
      @Override 
      public void run() { 
       try { 
        runnable.run(); 
       } finally { 

       } 
      } 
     }; 
     t.start(); 
     return t; 
    } 




    public class MyLocationListener implements LocationListener 
    { 

     public void onLocationChanged(final Location loc) 
     { 
      Log.i("***********************", "Location changed"); 
      if(isBetterLocation(loc, previousBestLocation)) { 
       loc.getLatitude(); 
       loc.getLongitude(); 
       intent.putExtra("Latitude", String.valueOf(loc.getLatitude())); //Double 
       intent.putExtra("Longitude", String.valueOf(loc.getLongitude())); //Double 
       intent.putExtra("Provider", loc.getProvider()); //String 
       intent.putExtra("Altitude", loc.getAltitude());//Double 
       intent.putExtra("Accuracy", loc.getAccuracy()); //float 
       intent.putExtra("Speed", loc.getSpeed()); //float 
       intent.putExtra("Bearing", loc.getBearing()); //float 
       Log.v("LOC", String.valueOf(loc.getLatitude())); 
       Log.v("LOC", String.valueOf(loc.getLongitude())); 
       Log.v("LOC", loc.getProvider()); 
       Log.v("LOC", String.valueOf(loc.getAltitude())); 
       Log.v("LOC", String.valueOf(loc.getAccuracy())); 
       Log.v("LOC", String.valueOf(loc.getSpeed())); 
       Log.v("LOC", String.valueOf(loc.getBearing())); 

       sendBroadcast(intent); 

      } 
     } 

     public void onProviderDisabled(String provider) 
     { 
      Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show(); 
     } 


     public void onProviderEnabled(String provider) 
     { 
      Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); 
     } 


     public void onStatusChanged(String provider, int status, Bundle extras) 
     { 

     } 

    } 
} 

調用Alarmreceiver在MainActivity

Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class); 
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0); 

manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent); 
+0

我可以看看你的兩個類的完整代碼嗎? –

+0

@SouravChandra當然,添加了必要的代碼。如果你想知道爲什麼這不是廣播接收器的一切,真的,沒有任何事情可以從服務中獲得intentextras。這是一個典型的'公共類AlarmReceiver通過'public void onReceive(Context context,Intent intent)擴展WakefulBroadcastReceiver' { –

+0

而不是創建一個新的Intent。您應該收到的意圖是 私人AlarmReceiver mMessageReceiver =新AlarmReceiver(){ @覆蓋 公共無效的onReceive(上下文語境,意圖意圖){列入意圖 } //提取數據} }; 假設AlarmReceiver擴展BroadcastReceiever。我不知道你是否關注這個問題。 –

回答

4

MainActivity類創建一個BroadcastReceiver對象如下:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     // Extract data included in the Intent 

    } 
}; 

而不是在MainActivity中使用定期檢查,而是創建一個Timer對象並使用sendBroadcast(intent)發送定時播放。

+0

Op已經提到'getIntent()。getExtras()。getString(「aKey」);'也行不通。 –

+1

@SouravChandra我可以在我的AlarmReceiver'BroadcastReceiver'中使用這個'BroadcastReceiver'嗎?如果我理解你的解決方案,我必須將這個意圖傳遞給這個私人的mMessageReceiver,提取它,然後以某種方式將它傳遞給另一個'BroadcastReceiver'? –

+0

沒有好友只是在你的MainActivity中使用片段。 sendBroadcast負責發送部分,接收是通過此BroadcastReceiver對象的參數完成的。 –

0

接收方必須有一個onreceieve方法。請使用它的意圖並使用intent.getStringExtra(「key」);並使用它。

+0

我嘗試在onReceive中調用上面的代碼(BroadcastReceiver中的Part)。你的意思是我不能在廣播接收機中創建新的意圖並從中獲得額外的內容? –