0

我試圖編寫一個數據共享應用程序,該應用程序將全天候運行,並且每當用戶在3分鐘內超過650米時繼續共享數據。位置BroadcastReceiver應用程序崩潰,如果minTime被設置爲0以外的任何其他值

錯誤是,如果我傳遞任何非零值,可以說,在minTime 18000,而不是傳遞0作品精美絕倫,如果minTime = 0

這裏是我的代碼的應用程序意外終止..

MainActivity.java

package com.example.agentbackgroundbroadcastreciever; 

    import android.location.LocationManager; 
    import android.location.Criteria; 
    import android.os.Bundle; 
    import android.app.Activity; 
    import android.app.PendingIntent; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.widget.Toast; 

    public class MainActivity extends Activity { 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      super.onCreate(savedInstanceState); 

      Intent intent = new Intent(MainActivity.this, LocationReceiver.class); 
      LocationManager manager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE); 
      long minTime = 18000; 
      float minDistance = 650; 
      String provider = LocationManager.GPS_PROVIDER; //GPS_PROVIDER or NETWORK_PROVIDER 

      Criteria criteria = new Criteria(); 
      criteria.setAccuracy(Criteria.ACCURACY_FINE); 


      PendingIntent launchIntent = PendingIntent.getBroadcast(this, 0, intent, 0); 

      manager.requestLocationUpdates(provider, 0, minDistance, launchIntent); ////Crashes if I put 18000 instead of 0 in minTime here 

     // Toast.makeText(MainActivity.this, "Came here", Toast.LENGTH_LONG).show(); 
     } 

    } 

LocationReceiver.java

package com.example.agentbackgroundbroadcastreciever; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.Toast; 


public class LocationReceiver extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 

     if(intent != null) 
     { 
     Bundle bundle = intent.getExtras(); 

      Location location = (Location) bundle.get(LocationManager.KEY_LOCATION_CHANGED); 
      Toast.makeText(context, "Received"+location.getLatitude()+" "+location.getLongitude(), Toast.LENGTH_LONG).show(); 
    //  DataSharing data=new DataSharing(); 
    //  data.sendSms(location.getLatitude(), location.getLongitude(), new java.util.Date()); 
     } 
    } 


} 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.agentbackgroundbroadcastreciever" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="19" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

     <receiver android:name="LocationReceiver"> 

    </receiver> 

     <activity 
      android:name="com.example.agentbackgroundbroadcastreciever.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

我找不出什麼問題。幫助將不勝感激。謝謝。

回答

0

如果(LocationManager.KEY_LOCATION_CHANGED)){}

,而不是 「如果(意向!= NULL)」

相關問題